Two’s Complement Calculator
Type a signed number to see its two’s complement bits, or paste a bit pattern to read it as a signed value.
8 bits hold −128 to 127.
Two’s complement, 8 bits
1101 0110
−42 in 8-bit two’s complement is 1101 0110 (hex D6).
- sign bit, worth −27
- value bits
Tap a bit to flip it.
- Hex
- 0xD6
- Read as unsigned
- 214
How two’s complement works
In an n-bit two’s complement number the top bit is worth −2ⁿ⁻¹ instead of +2ⁿ⁻¹; every other bit keeps its usual value. In 8 bits the top bit is −128, so 11010110 is −128 + 64 + 16 + 4 + 2 = −42.
To write a negative number by hand, write its absolute value in binary at the full width, flip every bit, and add 1. For −42 in 8 bits: 00101010 → 11010101 → 11010110. The same two steps turn a negative pattern back into its positive value.
The payoff is that addition needs no special cases: adding the patterns for 42 and −42 gives 1 00000000, and dropping the carry out of the top leaves 0. Every value has one pattern (there is no −0), and the range is lopsided by one: −128 to 127 in a byte.
value = −bₙ₋₁×2ⁿ⁻¹ + Σ bᵢ×2ⁱ (i < n−1); −x = (NOT x) + 1
- C23 and C++20 require signed integers to use two’s complement, ending the old allowance for ones’ complement and sign-magnitude. Source: WG14 N2412, two’s complement for C2x (adopted in C23).
Signed values in 8-bit two’s complement
Positive and negative values as 8-bit patterns, with the same value in 16-bit hex to show sign extension.
| Decimal | 8-bit binary | 8-bit hex | 16-bit hex |
|---|---|---|---|
| 127 | 0111 1111 | 7F | 007F |
| 100 | 0110 0100 | 64 | 0064 |
| 64 | 0100 0000 | 40 | 0040 |
| 42 | 0010 1010 | 2A | 002A |
| 1 | 0000 0001 | 01 | 0001 |
| 0 | 0000 0000 | 00 | 0000 |
| −1 | 1111 1111 | FF | FFFF |
| −2 | 1111 1110 | FE | FFFE |
| −10 | 1111 0110 | F6 | FFF6 |
| −42 | 1101 0110 | D6 | FFD6 |
Show all 14 rowsShow fewer
| −64 | 1100 0000 | C0 | FFC0 |
| −100 | 1001 1100 | 9C | FF9C |
| −127 | 1000 0001 | 81 | FF81 |
| −128 | 1000 0000 | 80 | FF80 |
Range by width
| Bits | Smallest | Largest | C type |
|---|---|---|---|
| 8 | −128 | 127 | int8_t |
| 16 | −32,768 | 32,767 | int16_t |
| 32 | −2,147,483,648 | 2,147,483,647 | int32_t |
| 64 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | int64_t |
Frequently Asked Questions
What is −1 in two’s complement?
All ones: 11111111 in 8 bits, FFFF in 16 bits, FFFFFFFF in 32 bits. Adding 1 to it overflows to all zeros, which is 0.
Why is the range −128 to 127 and not −127 to 127?
Zero takes one of the 128 patterns with a 0 on top, leaving 127 positives, while all 128 patterns with a 1 on top are negative. 10000000 is −128 and has no positive partner in 8 bits.
How do I extend a negative number to more bits?
Copy the sign bit into the new places on the left (sign extension). −42 is D6 in 8 bits and FFD6 in 16 bits; padding with zeros instead would give 214.
What is the difference between ones’ and two’s complement?
Ones’ complement negates by flipping the bits only, which leaves two zeros (00000000 and 11111111). Two’s complement adds 1 after flipping, giving one zero and simpler hardware.