two's complement
/ tooz KOM-pluh-muhnt /
Unsigned integers can only count upward from zero, but real programs need negative numbers too. The puzzle is: with only 0s and 1s and a fixed width, how do you write minus five? Two's complement is the elegant answer that virtually every modern computer uses, and once you see the trick it is hard to imagine anything cleaner.
The idea: keep the ordinary binary place values, but make the TOP bit's value negative. In an 8-bit number the bit positions are normally worth 128, 64, 32, 16, 8, 4, 2, 1; in two's complement the top one is worth MINUS 128 instead. So 1111 1111 is -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1, and 1000 0000 is plain -128. A handy shortcut to negate a number is: flip every bit, then add 1. To get -5, start from 5 (0000 0101), flip to 1111 1010, add 1 to get 1111 1011, which is indeed -5. An 8-bit signed range runs from -128 to +127.
Two's complement won because it makes the hardware simple: the SAME addition circuit works for signed and unsigned numbers — 0xFF + 0x01 = 0x00 whether you call 0xFF '255' or '-1' — and there is exactly one representation of zero (all bits 0), unlike older schemes. The price is a small asymmetry: there is one more negative number than positive, so the most negative value (like -128 in 8 bits, or INT_MIN) has no positive twin, and negating it overflows. That single quirk is the source of several real-world bugs, so it is worth remembering.
To get -5 in 8 bits: 5 is 0000 0101; flip all bits to 1111 1010; add 1 to get 1111 1011 = 0xFB. Check: 1111 1011 read as two's complement is -128+64+32+16+8+2+1 = -5.
Negate = flip all bits, then add one. The top bit carries negative weight.
Negating the most negative value (e.g. -INT_MIN) cannot be represented and is signed overflow — undefined behavior in C, even though the hardware would just give you the same negative number back.