Doing Sums in Binary
Computers add binary numbers with the same carry rules as decimal, but with only two digits. The core facts are , , , and (that is, carry ). Adding gives : the rightmost column is carry , the next column is carry , and the last column receives that carry to give .
Because a register holds a fixed number of bits, a sum that needs more bits than are available overflows — the extra high bit is lost. In an 8-bit register, wraps around to , because the true answer needs a 9th bit that does not exist. Overflow is not an error the hardware raises by default; it is silent, and reasoning about it is a real part of low-level programming.
Negative numbers use two's complement, the near-universal scheme for signed integers. To negate a number, invert all its bits and add 1. In 8-bit two's complement, the leftmost bit is the sign bit: means non-negative, means negative. This representation has one great virtue: the ordinary binary addition circuit works correctly for signed numbers too, with no special cases — subtraction becomes "add the two's complement."
| 8-bit pattern | Two's-complement value |
|---|---|
Common pitfall: assuming an integer can grow without bound. Machine integers have a fixed width, so they wrap on overflow — an 8-bit unsigned value silently goes , and a signed one goes . Forgetting this fixed range is the source of countless real-world bugs.