A value's bit width is how many bits it's stored in: a 4-bit value, an 8-bit register, a 32-bit bus. In hardware it's fixed when the circuit is built, and every result has to fit in it.
The width decides:
- the range: n bits make 2ⁿ patterns
- the MSB: bit n − 1, which is also the sign bit for signed values
- when overflow happens: a result outside the range wraps around
How many bits does a value need? Find the smallest n whose range contains it.
- Unsigned value v: the smallest n with 2ⁿ − 1 ≥ v.
- Two's complement value v: the smallest n with −2ⁿ⁻¹ ≤ v ≤ 2ⁿ⁻¹ − 1.
Watch exact powers of 2. 256 = 2⁸ needs 9 unsigned bits, because 8 bits stop at 255. And signed ranges are lopsided: −64 fits in 7 bits, but +64 needs 8.
To widen a value, add zeros on the left for unsigned numbers and use sign extension for two's complement.