Skip to content
KmapGen

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.

Input
Bit width

8-bit two's complement

1101 1011

0xDB

Signed value: -37 · Unsigned value: 219

Sign bit1 (negative)
Value from the bits−27 + 26 + 24 + 23 + 21 + 20 = -37
One's complement (inverted)0010 0100
One's complement representation1101 1010
Sign–magnitude1010 0101
Offset binary (excess-128)0101 1011
Range of 8-bit signed-128 to 127
Range of 8-bit unsigned0 to 255
Sign-extended to 16 bits1111 1111 1101 1011

Steps: -37 in 8-bit two's complement

  1. Write the magnitude 37 in binary: 0010 0101
  2. Invert every bit (one's complement): 1101 1010
  3. 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.

OperandBitsSigned
A0110 0100100
+ B0011 001050
= A + B1001 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

DecimalBinaryHex
127011111110x7F
64010000000x40
1000000010x01
0000000000x00
-1111111110xFF
-2111111100xFE
-37110110110xDB
-64110000000xC0
-127100000010x81
-128100000000x80

Worked example: −37 in 8 bits

  1. 37 in binary, padded to 8 bits: 00100101.
  2. Invert every bit: 11011010.
  3. Add 1: 11011011 (hex DB).
  4. 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 bitsRange (8 bits)Zeros
Two's complement11111011−128 to 127one
One's complement11111010−127 to 127+0 and −0
Sign–magnitude10000101−127 to 127+0 and −0
Offset binary (excess-128)01111011−128 to 127one

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.