Two's complement calculator

Convert a negative number to two's complement binary, or read two's complement bits back as a signed number. Pick 4 to 64 bits, and the calculator shows the hex, one's complement, signed range and every step.

One's complement

Show the working
Pick Binary or Hex to read two's complement bits back as a number.

Runs in your browser. Nothing you type is uploaded.

How to use the calculator

  1. Choose the Input: Decimal to get the two's complement of a number, or Binary or Hex to read bits back as a signed value.
  2. Pick the number of Bits. Auto uses the smallest of 4, 8, 16, 32 or 64 bits that fits a decimal number, and the number of bits you typed for binary or hex.
  3. Type the number, for example -10 or 11110110.
  4. Read the result, with the hex, unsigned value, one's complement, range and sign bit underneath. "Show the working" explains each step.

What two's complement is

Two's complement is how almost every computer stores whole numbers that can be negative. The first bit, the most significant bit, acts as the sign: 0 for zero and positive numbers, 1 for negative ones. In n bits the range is from -2n-1 to 2n-1 - 1, so one byte holds -128 to 127. Its big advantage is that the same circuit adds signed and unsigned numbers; there is no separate subtraction hardware and only one zero.

How to find the two's complement of a negative number

Converting -10 to 8-bit two's complement: 10 is 00001010, flipping the bits gives 11110101, and adding 1 gives 11110110. As a check, 256 - 10 = 246, which is 11110110 in binary.

The quickest check is subtraction: in n bits, a negative number x is stored as 2n - |x|. For -10 in 8 bits that is 256 - 10 = 246, or 11110110. Another shortcut works straight on the bits: copy them from the right up to and including the first 1, then flip everything to its left. 10 is 00001010; keep 10, flip 000010 to 111101, and you get 11110110 again.

How to convert two's complement to decimal

If the first bit is 0, read the number as ordinary binary. If it is 1, the number is negative, and there are three ways to get its value.

  • Give the first bit a negative weight. In 8 bits it is worth -128, so 11110110 is -128 + 64 + 32 + 16 + 4 + 2 = -10.
  • Or invert every bit (00001001), add 1 (00001010, which is 10) and put a minus sign in front: -10.
  • Or read the bits as unsigned (246) and subtract 2n, which is 256 for 8 bits.

Switch Input to Binary or Hex in the calculator to see the first two methods worked out for your own value. The binary to decimal and hex to decimal converters also have a Signed option.

A 4-bit example

Table of all sixteen 4-bit patterns from 0000 to 1111 with their unsigned values 0 to 15 and their two's complement values: 0 to 7, then -8 to -1 for patterns starting with 1.

With 4 bits there are 16 patterns. Read as unsigned they count from 0 to 15. Read as two's complement, 0000 to 0111 are 0 to 7 and 1000 to 1111 are -8 to -1. Counting up past 0111 wraps from 7 to -8, which is what an overflow looks like in a program.

Ranges by bit width

BitsSmallestLargestCommon type
4-87nibble
8-128127int8, signed char
16-32,76832,767int16, short
32-2,147,483,6482,147,483,647int32, int
64-9,223,372,036,854,775,8089,223,372,036,854,775,807int64, long long

Worked examples

Decimal8-bit16-bit16-bit hex
-1111111111111111111111111FFFF
-10111101101111111111110110FFF6
-100100111001111111110011100FF9C
-128100000001111111110000000FF80
127011111110000000001111111007F

Sign extension

To widen a two's complement number without changing its value, copy the sign bit into the new bits on the left. -10 in 8 bits is 11110110; in 16 bits it is 1111111111110110. Padding with zeros instead gives 0000000011110110, which is 246. That is why the byte F6 means -10 but the 16-bit value 00F6 means 246.

One's complement and two's complement

One's complement only flips the bits, so -10 in 8 bits would be 11110101. It has two zeros, 00000000 and 11111111, which makes arithmetic awkward. Adding 1 to the one's complement removes the second zero, and that is the two's complement used today.

Two's complement in Python, JavaScript, C and Excel

  • Python: format(-10 & 0xFF, '08b') returns '11110110'. Reading it back: int('11110110', 2) - 256 gives -10 when the first bit is 1.
  • JavaScript: (-10 & 0xFF).toString(2) returns "11110110", and (-10 >>> 0).toString(2) gives the 32-bit form.
  • C: (int8_t)0xF6 is -10.
  • Excel: =DEC2BIN(-10) returns the 10-bit form 1111110110, the two's complement output that Microsoft's DEC2BIN reference describes. For 8 bits, use =DEC2BIN(MOD(-10,256),8).

Frequently asked questions

What is 11111111 in two's complement?

-1 in 8 bits. Any width filled with 1s is -1.

What is the two's complement of 0001?

1111 in 4 bits, which is -1. Flip 0001 to 1110 and add 1.

What is -5 in 8-bit two's complement?

11111011. 5 is 00000101, flipped it is 11111010, and adding 1 gives 11111011.

How do I calculate two's complement in Excel?

=DEC2BIN(MOD(-10,256),8) gives the 8-bit pattern 11110110. DEC2BIN only accepts values up to 511, so for 16 bits use =DEC2HEX(MOD(-10,65536),4), which gives FFF6.

Why do computers use two's complement?

Addition and subtraction work the same way for signed and unsigned numbers, and there is only one zero.

Is my input sent anywhere?

No. The calculator runs in your browser.

Scroll to Top