One's complement, and two's complement binary codes (2024)

In fact, this calculator displays binary code for any integer number, but the code depends on a sign. For positive integers, the calculator displays their binary representation. For negative integers, the calculator displays their representation both as one's complement (also known as inverse code) and two's complement (or simply complement code).

You can find the text explaining the logic behind one's complement and two's complement codes below the calculator

One's complement and two's complement codes

Range

Binary code

One's complement (inverse code)

Two's complement (complement code)

One's complement and two's complement

Binary code is the binary representation of unsigned integers. If we're talking about computers, there is a certain number of bits (binary digits) used to represent the number. So, the total range which n bits can represent is One's complement, and two's complement binary codes (2)

One's complement or inverse code is the inverted binary code of a number. That is, all zeroes become ones and all ones become zeroes.

Two's complement or complement code is inverse code plus one

Now, what is it all about?

These codes were invented to make sign operations more comfortable (for machines). Since I'm the kind of person who likes to learn by example, I'll explain this by example.

Let's assume we have a computer with 4-bits binary numbers. A total range, which four bits can represent, is 16, starting from 0,1,... to 15. Here are the binary representations:
00 - 0000
...
15 - 1111

But these are unsigned numbers and are not of much use. We need to introduce a sign. So, let's take half of the range for positive numbers (eight, including zero), and half of the range - for negative (also eight). Note that the machine considers zero as a positive number, unlike usual math.

So, our positives will be 0,...,7, and negatives will be -1,...,-8.

To distinguish positive and negative numbers, we assign the left-most bit as sign bit. Zero in sign bit tells as that this is a positive number and one - negative.

Positive numbers are represented by plain binary code:
7 - 0111
6 - 0110
...
1 - 0001
0 - 0000

But how can negative numbers be represented? Here come the one's complement and two's complement codes.

Let's look at -7. Its absolute value is 7, which gives us 0111 in binary form. The one's complement is the inversion of bits of absolute value, where all 0 become 1 and all 1 become 0. So, -7's one's complement or inverse code is 1000. The two's complement is the inversion code plus one. So, -7's two's complement is 1001.

Note, that by itself, binary 1000 is 8, which, being added to 7 given 15, or One's complement, and two's complement binary codes (3). 15 is represented by 1111 (all bits are ones) in binary form, that's the name - one's complement - it "complements" binary code to One's complement, and two's complement binary codes (4), (all ones).

And binary 1001 is 9, which differs from -7 by 16, or One's complement, and two's complement binary codes (5). Or, which is the same, two's complement code "complements" binary code to One's complement, and two's complement binary codes (6), i.e. 7+9=16

The latter was proved to be very useful for machine computation - usage of two's complement code to represent negatives allows engineers to use an addition scheme for both addition and subtraction, thus simplifying the design of ALU (arithmetic and logical unit - a part of the processor). The two's complement code easily detects overflow, and the situation when there are not enough bits to represent the given number.

Several examples

7-3=4

NumberRepresented asBinary codeOne's complementTwo's complement
the summand701110111N/AN/A
the summand-31101001111001101
the sum401000100N/AN/A

-1+7=6

NumberRepresented asBinary codeOne's complementTwo's complement
the summand-11111000111101111
the summand701110111N/AN/A
the sum601100110N/AN/A

Overflow is detected by looking at the two last carries, including carrying beyond the right-most bit. If carry bits are 11 or 00, there is no overflow; if carry bits are 01 or 10, there is an overflow. And, if there is no overflow, carry beyond the right-most bit can be safely ignored.

Some examples with carries and a fifth bit (a bit beyond right-most bit)

7+1=8

NumberBinary code
the summand70111
the summand10001
the carries01110
the resultoverflow01000

The two last carries are 01. This gives a signal of overflow

-7+7=0

NumberBinary code
the summand-71001
the summand70111
the carries11110
the result010000

The result of addition 16 - but the fifth bit can be ignored; the real result is 0. The two last carries are 11. There is no overflow, so the correct result is indeed zero.

Overflow check can be done by simply XOR-ing two last carry bits.

Because of these convenient properties, two's complement is the most common method to represent negative numbers on computers.

P.S. The one's complement code also can be used to represent negatives, but the addition scheme should employ cyclic carry and is more complex. The range, which n bits can represent, is reduced by one since 1111 is busy as inverted 0000 - negative zero. So, it is less convenient.

One's complement, and two's complement binary codes (2024)

References

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5654

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.