Beetwise

Integer ranges by bit width

A register of n bits holds 2^n different values.

An unsigned register holds 0 to 2^n - 1.

A signed register holds -2^(n-1) to 2^(n-1) - 1, because one code belongs to zero.

Try integer ranges in the bit editor

The bits 10000000, the lowest 8-bit signed value. Edit the expression or select a bit to flip it.

7
6
5
4
3
2
1
0
-128
value · 8 bits · signed
hex
0x80
Read the bits as
Bit width

Integer ranges step by step

Read the bits in the editor as a value: The steps follow the value above.

  1. Write the bits: 10000000
  2. The top bit is 1, so the value is negative.
  3. Invert every bit: 01111111
  4. Add 1: 10000000, which is 128.
  5. The signed value is -128.

The range of each integer width

The range of each integer width
BitsUnsigned rangeSigned range
80 to 255-128 to 127
160 to 65535-32768 to 32767
320 to 4294967295-2147483648 to 2147483647
640 to 18446744073709551615-9223372036854775808 to 9223372036854775807

Where integer ranges is used

A driver picks the narrowest type that holds the full range of a sensor.

A counter that reaches its maximum wraps to zero, and a control loop then sees a large jump.

A protocol field of 16 bits cannot carry a value above 65535, so the format needs a wider field.

Integer ranges: points to note

  • The signed range is not symmetric. An 8-bit signed register holds -128 to 127, not -127 to 127.
  • A cast from a wider type to a narrower type drops the high bits. The value 300 in 8 bits becomes 44.
  • C leaves a signed overflow undefined. An unsigned overflow wraps, and the standard defines it.
  • The type char has a sign on some targets and none on others. State the type as int8_t or uint8_t.

The full bit editor

The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.

Open this value in the full editor

Questions about integer ranges

What is the largest 32-bit signed value?
The largest 32-bit signed value is 2147483647, which is 2^31 - 1.
Why is the lowest signed value one further from zero?
Zero takes one of the positive codes, so the negative side holds one more value than the positive side.
What happens when a counter passes its maximum?
An unsigned counter wraps to 0. The register drops the high bits that do not fit.