← All posts
November 15, 2026 · 3 min read

Two's complement: why -1 is 0xFFFFFFFF, and what unsigned changes

Two's complement explained on real bytes: why -1 is four bytes of ff, why the same bytes read as 4294967295 unsigned, and how -5 becomes fffffffb.

Two's complement is how your machine stores negative integers, and it explains a fact that surprises people the first time they print it: -1 stored in a 4-byte int is 0xFFFFFFFF, every bit set. Copy those exact bytes into an unsigned int and the same 32 bits read as 4294967295. Nothing in memory changed. Only the type did.

This is the written version of Understanding C++ #3. Everything below comes from one real program, compiled with g++ -std=c++20 -Wall -O2 on x86-64 Linux.

What does -1 look like in memory?

int s = -1; puts four bytes on the stack. In this run s lived at 0x7fffffffc130, and the four bytes from c130 to c133 are all ff. In binary, ff is 11111111, so all 32 bits of s are 1.

The stack at 0x7fffffffc130 holds int s as four bytes of ff, and below it all 32 bits are 1

That bit pattern is two's complement for -1.

How can all ones mean minus one?

In two's complement every bit has its usual binary weight, except the top one. For a 32-bit int:

bits weight
bit 31 (the sign bit) −2³¹ = −2147483648
bits 30 … 0 2³⁰, 2²⁹, …, 2, 1

With all 31 lower bits set, they add up to 2³¹ − 1 = 2147483647. Add the sign bit's −2³¹ and you get exactly −1.

The sign bit is worth -2^31, the other 31 bits add up to 2^31 - 1, together -1

So the sign bit is not a separate "minus" flag. It is a bit with a negative weight, and the hardware adds it like any other. That is why the same adder circuit handles signed and unsigned arithmetic.

What changes when the type is unsigned?

The program declares unsigned u; right after s (it landed at 0x7fffffffc134, the next four bytes) and copies the bytes over with std::memcpy(&u, &s, 4). The dump now shows eight bytes of ff: four for s, four for u.

Not one bit differs between them. The only difference is how the top bit is weighted: for unsigned it counts +2³¹ instead of −2³¹. So the value is 2³¹ + (2³¹ − 1) = 4294967295, about 4.3 billion, and that is exactly what printf("%u") prints.

unsigned u right after s holds the same four bytes of ff; its top bit counts +2^31, so the value is 4294967295

How do you negate a number in two's complement?

Flip every bit, then add one. Start from 5:

  • 5 is 00000000 00000000 00000000 00000101, or 0x00000005
  • ~5 flips every bit: 0xFFFFFFFA, which as an int is −6
  • ~5 + 1 is 0xFFFFFFFB, which is −5

The bits of ~5 + 1: ff ff ff fb, which is -5

Which byte comes first in memory?

The value is 0xFFFFFFFB, but x86-64 is little-endian: the lowest-order byte is stored at the lowest address. So after s = ~5 + 1; the bytes at c130…c133 read fb ff ff ff. The last line of the program reads the byte at &s directly through an unsigned char* and prints fb.

In memory s is fb ff ff ff: the low byte fb sits at the lowest address, 0x7fffffffc130

The program and its output

#include <cstdio>
#include <cstring>
int main() {
    int s = -1;
    std::printf("%p\n", (void*)&s);
    unsigned u;
    std::printf("%p\n", (void*)&u);
    std::memcpy(&u, &s, 4);      // same 4 bytes
    std::printf("%x\n", u);      // ffffffff
    std::printf("%d %u\n", s, u); // -1 4294967295
    s = ~5 + 1;                  // flip, add 1
    std::printf("%d\n", s);      // -5
    std::printf("%x\n", (unsigned)s); // fffffffb
    std::printf("%x\n", *(unsigned char*)&s);
}
0x7fffffffc130
0x7fffffffc134
ffffffff
-1 4294967295
-5
fffffffb
fb

The two addresses come from a run with address randomisation turned off (setarch -R); on your machine they will differ, and where the compiler puts u relative to s can change too. The other five lines are the same on any x86-64 compiler with a 4-byte int. Since C++20, two's complement is also what the standard requires for signed integers, so -1 is all ones everywhere.

The bytes never carry their sign. The type you read them through decides what they mean.

Try it

What is −2 in hex, as a 4-byte int? Work it out with flip-and-add-one from 0x00000002, then check it by adding a line to the program.