Two's Complement Calculator
Convert between decimal and two's complement binary or hex for any width from 2 to 256 bits, see the invert-and-add-one steps, and check additions and subtractions for overflow.
8-bit two's complement
1101 1011
0xDB
Signed value: -37 · Unsigned value: 219
| Sign bit | 1 (negative) |
|---|---|
| Value from the bits | −27 + 26 + 24 + 23 + 21 + 20 = -37 |
| One's complement (inverted) | 0010 0100 |
| One's complement representation | 1101 1010 |
| Sign–magnitude | 1010 0101 |
| Offset binary (excess-128) | 0101 1011 |
| Range of 8-bit signed | -128 to 127 |
| Range of 8-bit unsigned | 0 to 255 |
| Sign-extended to 16 bits | 1111 1111 1101 1011 |
Steps: -37 in 8-bit two's complement
- Write the magnitude 37 in binary: 0010 0101
- Invert every bit (one's complement): 1101 1010
- Add 1: 1101 1011
Check: 256 − 37 = 219, which is the unsigned reading of the same bits.
Add or subtract in two's complement
Uses the bit width chosen above. Operands may be negative or unsigned bit patterns.
| Operand | Bits | Signed |
|---|---|---|
| A | 0110 0100 | 100 |
| + B | 0011 0010 | 50 |
| = A + B | 1001 0110 | -106 |
- Result: 1001 0110 = -106 signed, 150 unsigned.
- Overflow (V): yes: the true result 150 is outside -128…127, so the signed answer wrapped around.
- Carry out (C): 0.
What two's complement is
Two's complement is how virtually every processor stores signed integers. In an n-bit number the top bit has the weight −2n−1 instead of +2n−1; all other bits keep their usual positive weights. So in 8 bits, 10000000 is −128 and 11111111 is −128 + 127 = −1.
Another way to see it: a negative number −x is stored as the unsigned number 2n − x. That is why −37 in 8 bits has the same bits as 256 − 37 = 219.
8-bit two's complement table
| Decimal | Binary | Hex |
|---|---|---|
| 127 | 01111111 | 0x7F |
| 64 | 01000000 | 0x40 |
| 1 | 00000001 | 0x01 |
| 0 | 00000000 | 0x00 |
| -1 | 11111111 | 0xFF |
| -2 | 11111110 | 0xFE |
| -37 | 11011011 | 0xDB |
| -64 | 11000000 | 0xC0 |
| -127 | 10000001 | 0x81 |
| -128 | 10000000 | 0x80 |
Worked example: −37 in 8 bits
- 37 in binary, padded to 8 bits:
00100101. - Invert every bit:
11011010. - Add 1:
11011011(hexDB). - Check: −128 + 64 + 16 + 8 + 2 + 1 = −37.
Sign extension
To widen a two's complement number, copy its sign bit into the new high bits: −37 is 11011011 in 8 bits and 1111 1111 1101 1011 (0xFFDB) in 16 bits. Padding with zeros instead would turn it into 219. The calculator shows the sign-extended form of every result.
Two's complement vs. other signed representations
| Representation | −5 in 8 bits | Range (8 bits) | Zeros |
|---|---|---|---|
| Two's complement | 11111011 | −128 to 127 | one |
| One's complement | 11111010 | −127 to 127 | +0 and −0 |
| Sign–magnitude | 10000101 | −127 to 127 | +0 and −0 |
| Offset binary (excess-128) | 01111011 | −128 to 127 | one |
The exponent field of an IEEE 754 float uses a biased (offset) code too, with a bias of 2k−1 − 1 instead of 2k−1.
Frequently asked questions
How do you find the two's complement of a number?
Write the magnitude in binary using the full bit width, invert every bit, then add 1. For −37 in 8 bits: 37 = 00100101, inverted 11011010, plus 1 = 11011011. Shortcut: copy the bits from the right up to and including the first 1, then invert all the bits to its left.
How do I convert two's complement binary back to decimal?
If the leftmost bit is 0, read the number as ordinary binary. If it is 1, the leftmost bit counts as −2n−1 instead of +2n−1: 11011011 = −128 + 64 + 16 + 8 + 2 + 1 = −37. Equivalently, subtract 2n from the unsigned value: 219 − 256 = −37.
What is the range of an n-bit two's complement number?
From −2n−1 to 2n−1 − 1. 8 bits: −128 to 127; 16 bits: −32,768 to 32,767; 32 bits: −2,147,483,648 to 2,147,483,647. The range is one larger on the negative side because zero has a single representation.
Why do computers use two's complement?
Because the same adder works for signed and unsigned numbers: adding the bit patterns gives the right answer in both interpretations, and subtraction is just adding the negated operand. There is only one zero (sign–magnitude and one's complement both have +0 and −0), and no end-around carry or sign handling is needed when adding numbers of different signs.
When does two's complement overflow?
Signed overflow happens when two numbers of the same sign are added and the result has the opposite sign, for example 100 + 50 in 8 bits gives 10010110 = −106. Adding numbers of opposite signs can never overflow. The carry out of the top bit is a different flag: it signals unsigned overflow, not signed overflow.
What is the two's complement of −128 in 8 bits?
−128 is 10000000. Inverting gives 01111111 and adding 1 gives 10000000 again: +128 does not exist in 8 bits, so negating the most negative number overflows back to itself. Code that computes abs(x) or −x on the minimum integer runs into this.
Related tools and guides
- Base converter Convert numbers between binary, octal, decimal, hexadecimal and any base from 2 to 36, including fractions with repeating digits, with steps.
- IEEE 754 converter IEEE 754 converter for single, double, half precision and bfloat16: decimal ↔ hex ↔ binary, clickable bits, the exact stored value and the rounding error.
- Logic gates All seven logic gates (AND, OR, NOT, NAND, NOR, XOR, XNOR) with ANSI and IEC symbols, truth tables, expressions and NAND/NOR-only circuits.
- Truth table generator Generate the truth table of any Boolean expression (AND, OR, NOT, XOR, NAND, NOR, →, ↔) with step columns, minterms, SOP/POS and CSV or LaTeX export.