One's complement calculator

Convert a decimal number to one's complement, flip the bits of any binary or hex number, or read one's complement bits back as a signed value in 4 to 64 bits. Switch to Add words to get the one's complement sum and checksum with every end-around carry shown.

Two's complement

Show the working
Pick Binary or Hex to flip the bits and read the signed value.

Runs in your browser. Nothing you type is uploaded.

How to use the calculator

  1. Choose the Input: Decimal to turn a number into one's complement, or Binary or Hex to flip the bits and read the 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 -25 or 0110.
  4. Read the result, with the hex, signed value, two's complement and range underneath. "Show the working" explains each step.

Set Mode to Add words to add binary or hex words with the end-around carry. Put one word on each line, pick the width (Auto uses 16 bits), and the calculator shows the one's complement sum and the checksum.

What one's complement is

The one's complement of a binary number is the same number with every bit flipped, so each 0 becomes 1 and each 1 becomes 0. The one's complement of 0110 is 1001, and the one's complement of 00101101 is 11010010. You will also see it written as ones' complement or 1's complement.

The name comes from subtraction. Flipping the bits gives the same answer as subtracting the number from a row of 1s of the same length. In 4 bits, 1111 - 0110 = 1001, which is 15 - 6 = 9.

One's complement is also a way of storing signed numbers. A positive number is plain binary with a 0 in the first bit, and its negative is the same pattern with every bit flipped. In 8 bits, +5 is 00000101 and -5 is 11111010. The first bit tells you the sign, the same as in two's complement.

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

  1. Write the positive value in binary and pad it with 0s to the bit width. For -25 in 8 bits, 25 is 00011001.
  2. Flip every bit. 00011001 becomes 11100110.

So -25 in 8-bit one's complement is 11100110, or E6 in hex. There is no "add 1" step. Adding 1 is what turns it into the two's complement, 11100111.

Pad before you flip. If you flip 11001 first and pad later, the leading 0s stay 0 and you end up with a positive number.

How to convert one's complement to decimal

  • If the first bit is 0, the number is positive. Read it as normal binary: 00011001 is 25.
  • If the first bit is 1, the number is negative. Flip every bit, read the result as binary and put a minus sign in front. 11100110 flips to 00011001, which is 25, so the value is -25.

The bit width matters. 1010 is -5 as a 4-bit one's complement number, but 00001010 is +10 in 8 bits. When you type binary into the calculator with Bits on Auto, it uses the length you typed.

Two zeros: +0 and -0

One's complement has two patterns for zero. In 8 bits, 00000000 is +0 and 11111111 is -0. They are the same number, so any hardware that compares values has to treat both as equal, and an addition can produce -0 where you expected 0. For example, 5 + (-5) in 8 bits is 00000101 + 11111010 = 11111111, which is -0.

This is the main reason computers moved to two's complement. Two's complement has one zero, gains one extra negative value (-128 instead of -127 in 8 bits) and adds without the end-around carry described below.

Ranges by bit width

With n bits, one's complement holds whole numbers from -(2n-1 - 1) to 2n-1 - 1. The range is symmetric because one pattern is spent on -0.

BitsSmallestLargestTwo's complement smallest
4-77-8
8-127127-128
16-32,76732,767-32,768
32-2,147,483,6472,147,483,647-2,147,483,648
64-9,223,372,036,854,775,8079,223,372,036,854,775,807-9,223,372,036,854,775,808

Worked examples

Decimal8-bit one's complementHex8-bit two's complement
5000001010500000101
-511111010FA11111011
-111111110FE11111111
-2511100110E611100111
-127100000008010000001
-011111111FFnone

Positive numbers look the same in both systems. Only the negatives differ, and the two's complement is always the one's complement plus 1.

One's complement addition and the end-around carry

Add the two numbers as ordinary binary. If a carry comes out of the leftmost bit, drop it from the left and add it back on the right. That second step is the end-around carry.

Take 5 + (-2) in 4 bits. 5 is 0101 and -2 is 1101. Their sum is 1 0010, five bits long. Move the leading 1 to the right end: 0010 + 1 = 0011, which is 3.

When there is no carry, the sum is already the answer. 2 + (-5) is 0010 + 1010 = 1100. The first bit is 1, so flip it to 0011 and read -3.

Binary subtraction works the same way. To get A - B, add A to the one's complement of B. For 7 - 3 in 4 bits: 0111 + 1100 = 1 0011, and the end-around carry gives 0100, which is 4.

One's complement in internet checksums

Very few computers store integers in one's complement today, but the IPv4 header, TCP and UDP checksums still use one's complement addition. RFC 1071 describes the method:

  1. Split the data into 16-bit words.
  2. Add the words with one's complement addition, folding every carry back into the low end.
  3. Flip the bits of the sum. That is the checksum.

With the words 8F2A and 9C11, the plain sum is 12B3B. The carry of 1 is added back to give 2B3C, and flipping 2B3C gives the checksum D4C3. The receiver adds every word including the checksum, and a clean packet comes out as FFFF, all 1s. Paste your own words into the calculator with Mode set to Add words to check them.

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

Most languages have a bitwise NOT operator, ~, which flips every bit. The catch is width: integers are 32 bits, 64 bits or unlimited, so you mask the result to the bits you want.

  • Python: format(~5 & 0xFF, '08b') returns '11111010'. On its own, ~5 prints -6, because Python shows the signed result of the flip.
  • JavaScript: (~5 & 0xFF).toString(2) returns "11111010".
  • C: uint8_t r = (uint8_t)~x; gives the 8-bit one's complement of x. With x = 5, r is 250.
  • Excel: =DEC2BIN(BITXOR(5,255),8) returns 11111010. XOR with 255 flips all 8 bits. Use 15 for 4 bits or 65535 for 16 bits.

Frequently asked questions

What is the 1's complement of 0110?

1001. Each bit is flipped.

What is the 1's complement of 7?

It depends on the bit width. 7 is 0111 in 4 bits, so its one's complement is 1000, which stands for -7. In 8 bits, 7 is 00000111 and the one's complement is 11111000.

What is the difference between one's complement and two's complement?

One's complement flips the bits. Two's complement flips the bits and adds 1. Two's complement has a single zero and one more negative number, and plain binary addition works on it without an end-around carry, which is why processors use it. The two's complement calculator shows both side by side.

Why does one's complement have two zeros?

Flipping 00000000 gives 11111111, and both have to mean zero. That costs one pattern, so the 8-bit range stops at -127 instead of -128.

Is one's complement still used?

For checksums, yes. IPv4, TCP and UDP all use the one's complement sum. Some older machines, such as the PDP-1, the CDC 6600 and the UNIVAC 1100 series, stored integers in one's complement, but current processors use two's complement.

Is one's complement the same as bitwise NOT?

Yes, for a fixed width. Bitwise NOT flips every bit, which is the one's complement. In a language that stores numbers in two's complement, ~x equals -x - 1, so ~5 is -6.

Is my input sent anywhere?

No. The calculator runs in your browser.

Scroll to Top