What is two’s complement?
Computers only store bits — 0s and 1s.
To represent both positive and negative integers, they use two’s complement encoding.
It’s called two’s complement because you take the binary complement (invert all bits) and then add 1.
invert all bits -> XOR 1
4️⃣ Range of representable numbers
For an n-bit two’s complement number:
- Minimum = −2^(n−1)
- Maximum = +2^(n−1) − 1
To find the value of a two’s complement binary:
If the MSB is 1:
- Invert all bits.
- Add 1.
- Put a minus sign.