Skip to content

Two’s Complement Calculator

Signed integers to two’s complement binary and hex and back, with steps, sign extension and overflow checks.

Your details

Operation

Results update as you type

Two’s complement (binary)

1111 1011

Hexadecimal
FB
Same bits read as unsigned
251
Ones’ complement (for comparison)
1111 1010
Sign-extended to double width
1111 1111 1111 1011
Range of this width
−128 … 127

Step-by-step solution
  1. |x| = 5 → binary 0000 0101
  2. Invert every bit (ones’ complement): 1111 1010
  3. Add 1: 1111 1011
  4. Check: 2^8 − 5 = 251 = FB₁₆
  5. Group by 4 bits for hex: 1111 1011 → FB

Almost every processor stores negative integers in two’s complement: the leftmost bit carries a negative weight, so the same adder circuit handles positive and negative numbers alike. This calculator shows how it works for 4-, 8-, 16-, 32- and 64-bit integers, using exact big-integer arithmetic.

It has three modes. Decimal → bits encodes a signed integer, showing the invert-and-add-one steps, the hex form, the ones’ complement for comparison and the value sign-extended to double width. Bits → decimal reads a binary or hex pattern back as a signed value. Add / subtract performs register arithmetic, shows the column addition, and flags signed overflow and the carry out separately — two things students often confuse.

For converting between arbitrary bases, including fractions, use the number base converter; for long binary arithmetic without a fixed width, the binary calculator.

How to use this calculator

  1. Choose the width: 8 bits for a byte, 16 for a short, 32 for an int, 64 for a long.
  2. Decimal → bits: type a whole number such as −5. Values outside the range are refused with the valid range.
  3. Bits → decimal: type a pattern such as 1111 1011 or 0xFB. Shorter patterns are padded with zeros.
  4. Add / subtract: type two integers within the range and pick + or −.
  5. Open the steps to follow each bit operation.

The formula

For an nn-bit word with bits bn−1…b0b_{n-1} \dots b_0, the signed value is:

x=−bn−1 2n−1+∑i=0n−2bi 2ix = -b_{n-1}\,2^{n-1} + \sum_{i=0}^{n-2} b_i\,2^i

The range is −2n−1≤x≤2n−1−1-2^{n-1} \le x \le 2^{n-1} - 1 (−128 to 127 for 8 bits). A negative xx is stored as the unsigned number 2n+x2^n + x, which is the same as inverting the bits of ∣x∣|x| and adding 1:

−x=x‾+1(mod2n)-x = \overline{x} + 1 \pmod{2^n}

Addition is done modulo 2n2^n. Signed overflow happens when two operands of the same sign give a result of the opposite sign; the carry out of the top bit is simply discarded.

Worked example

−5 in 8 bits: 5 = 0000 0101 → invert → 1111 1010 → add 1 → 1111 1011 = FB in hex. Check: 256−5=251=FB16256 - 5 = 251 = \text{FB}_{16}. Sign-extended to 16 bits it becomes 1111 1111 1111 1011.

127 + 1 in 8 bits: 0111 1111 + 0000 0001 = 1000 0000, read as −128: an overflow, since 128 does not fit. Likewise 100 + 50 gives −106, and −128 − 1 wraps to 127. And −1 in 16 bits is FFFF.

Two’s complement versus ones’ complement and sign-magnitude

8-bit system −5 Range Zero
Sign-magnitude 1000 0101 −127 … 127 two zeros (+0, −0)
Ones’ complement 1111 1010 −127 … 127 two zeros
Two’s complement 1111 1011 −128 … 127 one zero

Two’s complement wins because it has a single zero, one extra negative value, and needs no special case in the adder: subtraction is just addition of the negated operand. Since C23 and C++20, the C and C++ standards require it for signed integers.

Sign extension

To widen a signed value, copy its sign bit into the new upper bits. −5 is 1111 1011 in 8 bits and 1111 1111 1111 1011 in 16 bits; +5 stays 0000 0101 and gains zeros. Padding a negative pattern with zeros instead would turn −5 into +251, a classic bug when mixing signed and unsigned types.

Frequently asked questions

How do I find the two’s complement of a number?

Write the absolute value in binary with the chosen number of bits, invert every bit, then add 1. For −5 in 8 bits: 0000 0101 → 1111 1010 → 1111 1011.

How do I convert two’s complement back to decimal?

If the leftmost bit is 0, read the number normally. If it is 1, invert the bits, add 1, read that value and put a minus sign in front — or subtract 2^n from the unsigned value.

What is the range of an 8-bit or 32-bit signed integer?

8 bits: −128 to 127. 16 bits: −32,768 to 32,767. 32 bits: −2,147,483,648 to 2,147,483,647. In general −2^(n−1) to 2^(n−1) − 1.

How do I detect overflow when adding?

Overflow occurs only when both operands have the same sign and the result has the other sign, for example 127 + 1 = −128 in 8 bits. Adding numbers of opposite signs never overflows.

Is the carry the same as overflow?

No. The carry out of the top bit matters for unsigned arithmetic. For signed numbers, −1 + 1 produces a carry but no overflow, while 127 + 1 overflows without a carry.

Sources

Last updated:

More Math calculators

People also use