Signed vs Unsigned

What Do Signed and Unsigned Mean in Binary?

Binary is the same bits read two ways. Unsigned numbers hold zero and positive values, while signed numbers use the leftmost bit for negatives too.

On this page
  1. The short answer
  2. One pattern, two readings
  3. Why the signed range is shifted
  4. What it looks like in C++
  5. Summary

The short answer

Signed and unsigned describe how a fixed group of bits is read as a number. An unsigned number uses every bit for its magnitude, so it can only be zero or positive. A signed number uses the leftmost bit to show whether the value is negative, so the same group of bits can also stand for negative numbers. The bits don’t change. Only the reading does.

One pattern, two readings

Take four bits. Read them as unsigned and they run from 0 to 15. Read them as signed, using two’s complement, and they run from -8 to 7. Here is how a few patterns compare:

Bits Unsigned Signed
0000 0 0
0001 1 1
0111 7 7
1000 8 -8
1001 9 -7
1111 15 -1

Patterns that start with 1 are negative under the signed reading, which is why those rows look so different.

Why the signed range is shifted

Signed values split the same bits between negative and non-negative numbers. For a group of N bits, the unsigned range is 0 up to 2^N - 1. The signed range is -2^(N-1) up to 2^(N-1) - 1. For four bits, that gives 0 to 15 unsigned and -8 to 7 signed.

What it looks like in C++

In C++, int is signed by default, and unsigned int is its unsigned counterpart. The same bits print differently depending on the type:

#include <bitset>
#include <cstdint>
#include <iostream>

int main() {
    std::uint8_t unsigned_value = 0b10000000;  // 128
    std::int8_t signed_value = -128;           // same bits: 10000000

    std::cout << static_cast<int>(unsigned_value) << '\n';  // 128
    std::cout << static_cast<int>(signed_value) << '\n';    // -128
    std::cout << std::bitset<8>(signed_value) << '\n';      // 10000000
}

The bits are identical in both variables. Only the type changes the printed value.

The all-ones pattern is worth a closer look. For eight bits, it reads as 255 unsigned and -1 signed, as covered in what is 11111111 in signed binary. The trouble usually starts when a value moves between the two kinds, such as in a comparison. The guide to signed vs unsigned in c covers those cases in C++.

Summary

  • An unsigned reading uses every bit as magnitude, so the value is zero or positive.
  • A signed reading uses the leftmost bit to mark negative values in two’s complement.
  • The same bits can mean different numbers, depending on the type you read them with.

The complete guide to signed vs unsigned

Signed vs Unsigned Integers in C++
Read the guide

Keep exploring

Guides from other topics

All topics →