The short answer
A signed integer can hold negative numbers, zero and positive numbers. An unsigned integer holds only zero and positive numbers. With the same number of bits, an unsigned type gives up the negative range and gets a larger positive one. Its largest value is about twice the largest signed value.
Signed and unsigned types also differ in how they overflow, how they compare with each other, and how they behave with bit operations. Most surprises with these types come from one of those three areas.
What signed and unsigned mean
Both kinds store whole numbers in a fixed number of bits. The difference is how those bits are read. A signed type uses the leftmost bit to show whether the value is negative. An unsigned type uses every bit for magnitude, so its value is never negative.
For the bit-level basics before any C++, the article on what does signed and unsigned mean in binary is a good place to start.
Ranges for the same number of bits
| Type | Typical size | Smallest value | Largest value |
|---|---|---|---|
signed char |
8 bits | -128 | 127 |
unsigned char |
8 bits | 0 | 255 |
int |
32 bits | -2,147,483,648 | 2,147,483,647 |
unsigned int |
32 bits | 0 | 4,294,967,295 |
The size of int depends on the platform. The C++ standard sets only minimum sizes, and 32 bits is common on current desktop and server systems. To check your own system, query std::numeric_limits, as the wraparound example below does.
The integer types short, int, long and long long each come in a signed and an unsigned form. The char family is a little different. char, signed char and unsigned char are three separate types, and whether plain char is signed depends on the compiler. If the primitive data types in c are new to you, think of these pairs as part of the same set. Wider types, such as the one covered in what is long long in c, follow the same rules.
Declaring signed and unsigned types
int temperature = -5; // signed; int is signed by default
unsigned int count = 5u; // unsigned; the u suffix makes the literal unsigned
signed char small = -1; // explicitly signed
unsigned char byte = 255; // one byte, from 0 to 255
unsigned on its own means unsigned int, and signed on its own means signed int.
When the exact width matters, such as with binary file formats or hardware registers, the fixed-width types in <cstdint> are clearer. std::int32_t and std::uint32_t are exactly 32 bits on platforms that provide them, which covers mainstream systems. std::uint8_t is a common choice for raw bytes.
How the sign bit works
Since C++20, signed integers use two’s complement. In that scheme, a value with a leftmost bit of 1 is negative. To find its value, read the bits as unsigned and subtract 2 to the power of the bit count. In an 8-bit byte, 11111111 reads as 255 unsigned, and 255 - 256 gives -1 as a signed value.
The pattern 11111111 is worked through step by step in the piece on what is 11111111 in signed binary.
Before C++20, the standard also allowed other ways to store negative numbers. Modern C++ settles that question in favor of two’s complement, so the same bits mean the same value on every compliant compiler.
Wraparound and overflow
Unsigned arithmetic wraps around. Results are kept modulo 2 to the power of the bit count, so subtracting 1 from zero gives the largest value the type can hold.
#include <iostream>
#include <limits>
int main() {
unsigned int zero = 0;
unsigned int wrapped = zero - 1;
std::cout << wrapped << '\n';
std::cout << std::numeric_limits<unsigned int>::max() << '\n';
}
On a system where unsigned int is 32 bits, both lines print 4294967295.
Signed overflow is a different matter. In C++, signed integer overflow is undefined behavior. The compiler may assume it never happens, so the result can change with the compiler or its optimization settings. Do not rely on a signed value wrapping, even if a quick test seems to show it.
int largest = std::numeric_limits<int>::max();
int broken = largest + 1; // undefined behavior
If you need wraparound, use an unsigned type on purpose. If you need to detect overflow, check the values before you do the arithmetic.
Comparing signed and unsigned values
When a signed and an unsigned operand meet in a comparison, the signed operand is converted to unsigned first. A negative value turns into a very large positive one, which is why the next example behaves in a way many readers find surprising.
#include <iostream>
int main() {
int a = -1;
unsigned int b = 1;
if (a < b) {
std::cout << "a is smaller\n";
} else {
std::cout << "a is not smaller\n"; // this line runs
}
}
GCC and Clang can report this with -Wsign-compare, which -Wextra also turns on. Turn on warnings and read them, and consider treating them as errors in projects where that is practical.
Loops and counters
An unsigned counter that counts down past zero never becomes negative. It wraps instead, so a loop condition like i >= 0 is always true.
// Does not stop: after 0, --i wraps to the largest unsigned value
for (unsigned int i = 3; i >= 0; --i) {
std::cout << i << '\n';
}
Counting down with a signed int avoids this, because the condition can become false.
Results from the standard library
Many standard library functions return unsigned types. The size() member function of std::vector returns an unsigned type, usually std::size_t, and sizeof also produces a std::size_t. Readers who are still getting used to the sizeof function in c will find its result is unsigned too.
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{10, 20, 30};
for (int i = 0; i < values.size(); ++i) { // warns: signed and unsigned comparison
std::cout << values[i] << '\n';
}
for (std::size_t i = 0; i < values.size(); ++i) { // same type as size()
std::cout << values[i] << '\n';
}
}
Subtraction needs care too. values.size() - 1 on an empty vector wraps to a huge value instead of -1. For a closer look at what size() returns, read how does size() work in C++.
Bit manipulation and unsigned types
Bit masks, flags and shifts are where unsigned types are usually the better choice. An unsigned value is just a pattern of bits, so masking and shifting behave predictably. Right-shifting a negative signed value depends on the implementation, which makes signed types a poor fit for that work.
The shift operator in c is where this shows up most often.
#include <cstdint>
#include <iostream>
int main() {
std::uint32_t flags = 0;
flags |= 1u << 3; // set bit 3
bool bit_three = (flags & (1u << 3)) != 0;
std::cout << std::boolalpha << bit_three << '\n'; // true
}
Choosing between them
- Use signed types, such as
int, for ordinary arithmetic, especially when a value can go below zero. The C++ Core Guidelines suggest signed types for arithmetic and unsigned types for bit manipulation. - Use unsigned types for bit masks, flags, hashes and raw bytes, or when wraparound is the intent.
- Do not use an unsigned type only to say that a value can’t be negative. Subtraction and comparisons can then produce results that are hard to spot.
- Avoid mixing signed and unsigned operands in one expression. When you must, make the conversion explicit with
static_castand a comment that explains why. - Turn on compiler warnings and read them.
Common mistakes
- Expecting unsigned subtraction to produce a negative number.
- Comparing an
intwithsize()without a cast. - Assuming signed overflow wraps around.
- Assuming plain
charis always signed. Its signedness depends on the compiler. - Printing a value stored in an
unsigned intthat was meant to be negative, and expecting a minus sign.
Summary
Signed types hold negative, zero and positive values. Unsigned types hold zero and positive values, and in the same width they reach about twice as high. Unsigned arithmetic wraps, while signed overflow is undefined. Mixed comparisons convert the signed side to unsigned. Choose the type that matches the job, and let compiler warnings guide you.