A computer's memory is just rows of switches: each one is a 1 or a 0, with no little minus sign to spare. So how does a byte hold -45? You cannot glue a "-" onto binary. Instead computers agree on a clever code called two's complement, where the top bit quietly does the job of the minus sign. Learn one rule — invert and add 1 — and you can flip any number positive or negative in seconds.
Why negatives need a system
In plain (unsigned) binary, 8 bits store 0 to 255 — all positive. To fit negatives in the same 8 bits we sacrifice the top bit as a sign: if it is 1 the number is negative. Two's complement is the scheme that makes this work so that ordinary binary addition still gives the right answer.
- The most significant bit (MSB) carries a weight of -128 instead of +128.
- A number is negative if and only if the MSB is 1.
- The 8-bit range is -128 to +127 (that is 256 different values).
The rule: invert and add 1
To find the negative of a number: write it in positive binary, flip every bit (0↔1), then add 1. The very same rule works in reverse to turn a negative back into a positive so you can read its size.
| Denary | 8-bit two's complement |
|---|---|
| +127 | 0111 1111 |
| +1 | 0000 0001 |
| 0 | 0000 0000 |
| -1 | 1111 1111 |
| -45 | 1101 0011 |
| -128 | 1000 0000 |
Reading a two's complement number
To read a byte whose MSB is 1, either add up the weights (remembering the top column is -128) or apply invert-and-add-1 to find its positive twin. For 1101 0011: -128 + 64 + 16 + 2 + 1 = -45.
Step 1 — write +75 in binary: 64+8+2+1, so 0100 1011.
Step 2 — invert every bit: 1011 0100.
Step 3 — add 1: 1011 0101.
Check: -128 + 32 + 16 + 4 + 1 = -75. Answer: 1011 0101.
Memory hook: "Flip, then tip" — flip all the bits, then tip in a 1. And the range is easy: the top value is one less than 128, so it is -128 to +127.