Two’s complement calculator
This tool shows how a signed decimal integer is stored in two’s complement form at 8, 16, 32, or 64 bits, with the binary pattern, the hexadecimal value, and both the unsigned and signed readings of the same bits. It is for programmers, students, and anyone debugging low-level numeric encodings — particularly useful when reading memory dumps, writing embedded C, inspecting wire protocols, or understanding why a C int cast behaves unexpectedly.
How it works
Two’s complement is how nearly every modern CPU represents negative integers. For a width of w bits, a negative value −n is stored as 2^w − n, which makes the most significant bit act as a sign bit and lets addition and subtraction share one circuit. The tool masks your value to w bits and then:
binary = the w-bit pattern of (value mod 2^w)
hex = that pattern in base 16
unsigned = the pattern read as a non-negative number (0 … 2^w − 1)
signed = the pattern read in two's complement (−2^(w−1) … 2^(w−1) − 1)
Each width has a fixed signed range; values outside it are flagged and shown wrapped. Big-integer arithmetic keeps even 64-bit results exact, avoiding the floating-point precision loss that would otherwise affect values larger than 2^53.
Worked examples
Example 1 — negative number at 8 bits
For −42 at 8 bits: 2^8 − 42 = 256 − 42 = 214, whose 8-bit pattern is 11010110 (hex D6). Read as unsigned that is 214; read as signed two’s complement it is −42. Notice the most-significant bit is 1, which is always the sign-bit marker in two’s complement.
Example 2 — range overflow at 8 bits
Enter 200 at 8 bits. The signed range for 8-bit is −128 to 127, so 200 overflows. The tool shows the wrapped value of 200 − 256 = −56, confirming how unsigned byte 200 reinterprets as signed −56 in C when you cast (int8_t)200.
Example 3 — finding the complement by hand
The long-hand method: take the absolute value (42 → 00101010), invert every bit (11010101), then add 1 (11010110). The result matches the calculator’s D6 exactly. The formula 2^w − n and the invert-then-add-1 rule are equivalent — the tool confirms both.
Quick reference: signed ranges
| Width | Signed range | Unsigned range |
|---|---|---|
| 8-bit | −128 … 127 | 0 … 255 |
| 16-bit | −32,768 … 32,767 | 0 … 65,535 |
| 32-bit | −2,147,483,648 … 2,147,483,647 | 0 … 4,294,967,295 |
| 64-bit | −9.22 × 10^18 … 9.22 × 10^18 | 0 … 18.44 × 10^18 |
When bit-width matters
Pick the width to match your target type: int8_t → 8 bits, short or int16_t → 16 bits, int or int32_t → 32 bits, long long or int64_t → 64 bits. Choosing the wrong width is a common source of silent truncation bugs — the tool shows the wrapped result so you can see exactly what the hardware stores.
Everything runs locally in your browser — nothing is uploaded.