Advertisement

One's complement calculator

Enter a whole number in decimal or binary and choose a bit width. The one's complement flips every bit, including the sign bit.

Calculator

Numbers are stored without a sign unless a result is reported as signed.

The signed range is -(2ⁿ⁻¹ - 1) to +(2ⁿ⁻¹ - 1). One's complement has both positive and negative zero.

Entered value in binary
00000101
One's complement in binary
11111010
Entered value as signed decimal
5
Complement as signed decimal
-5
Complement as unsigned decimal
250

Advertisement

How this calculator works

One's complement writes a negative number by flipping every bit of its positive counterpart. In 8 bits, 5 is 00000101 and -5 is the complement 11111010. Flipping again returns the original number. The leading bit acts as a sign: 0 for positive, 1 for negative. Because the sign bit is flipped along with the other bits, the all-ones pattern represents negative zero, which has no separate positive value. That leaves one fewer negative value than positive value at every width. The signed readings on this page use exactly that convention, not two's complement.

Formula

In n bits, the one's complement is (2ⁿ - 1) - x, which is the same as flipping every bit. A value x is representable when -(2ⁿ⁻¹ - 1) ≤ x ≤ 2ⁿ⁻¹ - 1. The negative representation of x is the complement of |x|.

Worked example

In 8 bits, 5 is 00000101 and its complement is 11111010, which reads as -5. The complement of -5 flips back to 00000101, or 5. In 4 bits, 1010 has a leading 1, so it reads as -(1111 - 1010) = -(0101) = -5, and its complement 0101 reads as 5.

Advertisement

Frequently asked questions

Why does one's complement have two zeros?

Flipping 00000000 gives 11111111. If both patterns are allowed, one means positive zero and the other means negative zero, even though they describe the same value. Most hardware avoids this ambiguity by using two's complement instead.

What is the range for 8 bits?

The positive range is 0 to 127 and the negative range is -1 to -127, plus negative zero. The value -128 cannot be written in one's complement in 8 bits because its magnitude 128 needs eight value bits and leaves no separate sign position.

How is this different from two's complement?

Two's complement adds 1 after flipping the bits. That removes negative zero and extends the negative range by one value, which is why -128 fits in 8-bit two's complement. One's complement is the flip alone.

What happens when I enter binary digits?

The digits are padded on the left with zeros to the chosen width, then read in one's complement. For example, 1010 in 4 bits has a leading 1 and reads as -5. Flipping it gives 0101, or 5.

Sources

Advertisement