A fixed number of bits can only make so many patterns (2ⁿ for n bits), so every format has a range: the values it can show. Any result outside the range causes overflow.
The formulas for n bits:
- Unsigned: 0 to 2ⁿ − 1
- Two's complement: −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1
- Sign-magnitude: −(2ⁿ⁻¹ − 1) to 2ⁿ⁻¹ − 1, with two zeros
Some values to know:
- 4 bits: 0 to 15 unsigned, −8 to 7 signed
- 8 bits: 0 to 255 unsigned, −128 to 127 signed
- 16 bits: 0 to 65,535 unsigned, −32,768 to 32,767 signed
Why the − 1s? Unsigned: zero uses up one of the 2ⁿ patterns. Two's complement: half the patterns (MSB 1) are negative, and the other half hold zero and the positives, so the positive side ends one short. That's why −128 fits in 8 bits but +128 doesn't.
To find how many bits a value needs, look for the smallest n whose range contains it (see bit width).