± Signed Number Representations
How computers represent negative numbers: sign-magnitude, one's complement, and two's complement. Two's complement is the dominant standard in modern computing.
Interactive Representation Explorer
Representation Methods
Sign-Magnitude
MSB = sign, rest = magnitude
+5 = 0101
-5 = 1101
One's Complement
Negate by flipping all bits
+5 = 0101
-5 = 1010 (flip bits of +5)
Two's Complement
Negate: flip bits + 1
+5 = 0101
-5 = 1011 (flip bits + 1)
Range of Representable Numbers
| Representation | 4-bit | 8-bit | 16-bit | Notes |
|---|---|---|---|---|
| Sign-Magnitude | [-7, +7] | [-127, +127] | [-32767, +32767] | Two zeros (+0, -0) |
| One's Complement | [-7, +7] | [-127, +127] | [-32767, +32767] | Two zeros (+0, -0) |
| Two's Complement | [-8, +7] | [-128, +127] | [-32768, +32767] | One zero, asymmetric range |
Two's Complement Advantage
Addition & Overflow Detection
Two's complement addition
0011 = 3
+ 0101 = 5
1000 = 8
Overflow detected! Signs of A and B match but result sign differs.
Overflow Detection Rule
Detailed Comparison Table (4-bit)
| Decimal | Sign-Mag | One's Comp | Two's Comp |
|---|---|---|---|
| 7 | 0111 | 0111 | 0111 |
| 6 | 0110 | 0110 | 0110 |
| 5 | 0101 | 0101 | 0101 |
| 4 | 0100 | 0100 | 0100 |
| 3 | 0011 | 0011 | 0011 |
| 2 | 0010 | 0010 | 0010 |
| 1 | 0001 | 0001 | 0001 |
| 0 | 0000 | 0000 | 0000 |
| 0 | 0000 | 0000 | 0000 |
| -1 | 1001 | 1110 | 1111 |
| -2 | 1010 | 1101 | 1110 |
| -3 | 1011 | 1100 | 1101 |
| -4 | 1100 | 1011 | 1100 |
| -5 | 1101 | 1010 | 1011 |
| -6 | 1110 | 1001 | 1010 |
| -7 | 1111 | 1000 | 1001 |
| -8 | — | — | 1000 |
Code Example
python
def twos_complement(val: int, bits: int = 8) -> str:
"""Convert signed integer to two's complement binary string."""
mask = (1 << bits) - 1
masked = val & mask if val < 0 else val
return format(masked, f'0{bits}b')
def from_twos_complement(bin_str: str) -> int:
"""Convert two's complement binary string to signed integer."""
bits = len(bin_str)
val = int(bin_str, 2)
if bin_str[0] == '1':
val -= (1 << bits)
return val
def detect_overflow(a_bin: str, b_bin: str, result_bin: str) -> bool:
"""Detect signed overflow in two's complement addition."""
a_sign, b_sign, r_sign = a_bin[0], b_bin[0], result_bin[0]
return a_sign == b_sign and a_sign != r_sign
# Examples
print(twos_complement(-5, 4)) # 1011
print(from_twos_complement("1011")) # -5
print(detect_overflow("0111", "0010", "1001")) # True (7+2=-7)Interview Questions
Why do most computers use two's complement instead of sign-magnitude?
Two's complement has several advantages: (1) only one representation for zero, avoiding the ambiguity of +0 and -0; (2) addition and subtraction hardware works identically for signed and unsigned numbers; (3) the ALU doesn't need special logic for signed operations; (4) the asymmetric range gives one extra negative value (-2^{n-1}).
How do you negate a number in two's complement?
To negate a two's complement number, invert all bits (bitwise NOT) and add 1. This is equivalent to taking the one's complement and adding 1. For example, +5 (0101) becomes -5: invert to 1010, add 1 → 1011.
What is the range of signed numbers in 8-bit two's complement and why is it asymmetric?
8-bit two's complement ranges from -128 to +127. It is asymmetric because zero takes one slot in the positive range. With 8 bits there are 2^8 = 256 possible values. If one is zero, the remaining 255 are split between negative and positive. Since the MSB is the sign bit, -128 (10000000) is a valid value with no positive counterpart.