Beetwise

Signed and unsigned values

An unsigned value uses all its bits for the magnitude.

A signed value uses the top bit to show a negative value.

The same bits give two different values. Only the interpretation changes.

Try signed and unsigned values in the bit editor

The bits 11111010, which read as 250 or as -6. Edit the expression or select a bit to flip it.

7
6
5
4
3
2
1
0
250
value · 8 bits · unsigned
hex
0xFA
Read the bits as
Bit width

Signed and unsigned values step by step

Read the bits 11111010 in both modes: The steps follow the value above.

  1. Write the bits: 11111010
  2. The register holds an unsigned value, so every bit is part of the magnitude.
  3. Add the powers of two of the bits with the value 1. The value is 250.
  4. Switch the mode to signed. The same bits then read as a negative value.

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 signed and unsigned values is used

A sensor reading is often signed, because a temperature or a speed goes below zero.

A status register is almost always unsigned, because each bit is a flag.

A wrong mode gives a large positive value in place of a small negative value.

Signed and unsigned values: points to note

  • An 8-bit unsigned value holds 0 to 255. An 8-bit signed value holds -128 to 127.
  • A 32-bit unsigned value holds 0 to 4294967295. A 32-bit signed value holds -2147483648 to 2147483647.
  • A comparison between a signed value and an unsigned value is a common source of defects. The compiler converts one of them first.

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 signed and unsigned values

Do the bits change when I change the mode?
No. The bits stay the same. Only the value that you read from them changes.
Which mode does a C int use?
A plain int holds a signed value. Use unsigned int, or a type such as uint32_t, for an unsigned value.
Why is the negative range one larger?
Zero uses one code in the positive half. The negative half therefore holds one more value.