Sub topic 8
Two's complement arithmetic refresher
This chapter assumes you can convert between binary and hexadecimal and know how a full adder works. It covers the representation the ALU, status flags and GATE numerical problems all rely on.
Why two's complement
A computer needs one adder circuit that handles both signed and unsigned numbers. Two's complement allows this. An n-bit pattern is interpreted in one of two ways, and the same addition hardware produces a correct bit pattern either way.
For an n-bit pattern with bits b(n-1) ... b0, the signed value is:
value = -b(n-1) × 2^(n-1) + (sum of b(i) × 2^i for i = 0 to n-2)
The top bit has a negative weight. All other bits have the usual positive weights.
| Scheme | Range for n bits | Zeros | Adder works unchanged? |
|---|---|---|---|
| Sign-magnitude | -(2^(n-1) - 1) to 2^(n-1) - 1 | Two (+0 and -0) | No, sign must be handled separately |
| One's complement | -(2^(n-1) - 1) to 2^(n-1) - 1 | Two | Needs an end-around carry |
| Two's complement | -2^(n-1) to 2^(n-1) - 1 | One | Yes |
Range and the asymmetry
An n-bit two's complement number covers -2^(n-1) to 2^(n-1) - 1. There are 2^n patterns, one is zero, and half of the remaining patterns are negative. That leaves one more negative value than positive values.
| 4-bit pattern | Unsigned | Signed |
|---|---|---|
| 0000 | 0 | 0 |
| 0111 | 7 | 7 |
| 1000 | 8 | -8 |
| 1111 | 15 | -1 |
The sign of a number is read from its most significant bit. A set bit means negative.
Negation
To negate a number, invert every bit and add 1. Equivalently, the negative of x is 2^n - x. Take +10 in 8 bits:
- 10 = 0000 1010.
- Invert: 1111 0101.
- Add 1: 1111 0110 = 0xF6, which is 246 unsigned and -10 signed.
A quick check is that 0xF6 + 0x0A = 0x100, so the 8-bit result is 0.
A manual shortcut for hand calculation: copy bits from the right up to and including the first 1, then invert all bits to the left of it.
The most negative value has no positive partner. Negating 1000 0000 (-128) gives 1000 0000 again, because inverting yields 0111 1111 and adding 1 returns to 1000 0000.
Sign extension
To widen a signed number, copy the sign bit into the new upper bits. Zero extension is for unsigned values only.
- 0xF6 (-10) sign-extended from 8 to 16 bits is 0xFFF6, still -10.
- 0xF6 zero-extended is 0x00F6, which is 246.
Mixing the two up is a common source of wrong answers in problems about loading bytes into wider registers.
Addition and subtraction on one adder
Addition is plain binary addition, and any carry out of the top bit is discarded when you read the result as an n-bit value. Subtraction reuses the adder:
A - B = A + (NOT B) + 1
Hardware implements this with an XOR gate on each bit of B and a control line called Sub. When Sub is 1, every bit of B is inverted and the same line feeds the adder's carry-in.
Carry versus overflow
These two are different conditions, and GATE questions often test the difference.
- Carry is the carry out of the most significant bit. It signals an unsigned result that does not fit in n bits.
- Overflow signals a signed result that does not fit in n bits.
For addition, overflow occurs when both operands have the same sign and the result has the opposite sign. Equivalently, the carry into the top bit differs from the carry out of the top bit. Adding numbers of opposite signs can never overflow.
For subtraction A - B, overflow occurs when A and B have different signs and the result's sign differs from A's.
Working code
The following program shows conversion, negation, sign extension and subtraction with flags.
def to_signed(value, bits):
value &= (1 << bits) - 1
return value - (1 << bits) if value >> (bits - 1) else value
def negate(value, bits):
return (~value + 1) & ((1 << bits) - 1)
def sign_extend(value, from_bits, to_bits):
value &= (1 << from_bits) - 1
if value >> (from_bits - 1):
value |= ((1 << to_bits) - 1) ^ ((1 << from_bits) - 1)
return value
def sub(a, b, bits):
"""Compute a - b as a + ~b + 1. Returns (result, carry, overflow)."""
mask = (1 << bits) - 1
total = a + (~b & mask) + 1
result = total & mask
carry = total >> bits
sa = (a >> (bits - 1)) & 1
sb = (b >> (bits - 1)) & 1
sr = (result >> (bits - 1)) & 1
overflow = int(sa != sb and sr != sa)
return result, carry, overflow
print(to_signed(0xF6, 8)) # -10
print(negate(10, 8)) # 246 (0xF6)
print(negate(0x80, 8)) # 128 (0x80): -128 negates to itself
print(hex(sign_extend(0xF6, 8, 16))) # 0xfff6
print(sub(5, 7, 8)) # (254, 0, 0): -2, carry 0
print(sub(0x80, 1, 8)) # (127, 1, 1): -128 - 1 overflowsIn sub(5, 7, 8) the carry is 0 even though the answer is correct as a signed number. For subtraction done this way, carry-out 1 means no borrow was needed (A ≥ B as unsigned). Some architectures store the inverted value as a borrow flag, so check the machine's definition before answering a question.
Common errors
- Reading carry as overflow. Adding 0xFF and 0x01 in 8 bits gives carry 1 but no signed overflow, because -1 + 1 = 0.
- Forgetting the +1 when negating, which gives the one's complement instead.
- Using zero extension on signed data or sign extension on unsigned data.
- Assuming the range is symmetric. The minimum is -2^(n-1), and its negation overflows.
- Testing overflow on mixed-sign addition. It can only happen when the operand signs match.
Exercise
An 8-bit ALU adds 0x9C and 0xB5. Give the 8-bit result in hexadecimal, the carry-out, and the signed overflow flag. State the decimal signed values of the operands and result.
Show answer
0x9C + 0xB5 = 0x151, so the 8-bit result is 0x51 (81) and the carry-out is 1. Signed, 0x9C is -100 and 0xB5 is -75, and 0x51 is +81. Both operands are negative and the result is positive, so overflow is 1. The true sum, -175, is below -128.
Exercise
What is the smallest number of bits n that can represent every integer from -300 to 300 in two's complement?
Show answer
The range must satisfy 2^(n-1) ≥ 301, because the maximum is 2^(n-1) - 1 and the minimum is -2^(n-1). 2^8 = 256 is too small and 2^9 = 512 works, so n - 1 = 9 and n = 10 bits (range -512 to 511).