One's complement, and two's complement binary codes

This online calculator displays one's complement and two's complement codes for the entered negative integer.

This page exists due to the efforts of the following people:

Timur

Timur

Created: 2010-09-14 07:01:35, Last updated: 2021-09-30 11:50:27

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

PLANETCALC, One's complement and two's complement codes

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 2^n

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 2^n-1. 15 is represented by 1111 (all bits are ones) in binary form, that's the name - one's complement - it "complements" binary code to 2^n-1, (all ones).

And binary 1001 is 9, which differs from -7 by 16, or 2^4. Or, which is the same, two's complement code "complements" binary code to 2^n, 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

Number Represented as Binary code One's complement Two's complement
the summand 7 0111 0111 N/A N/A
the summand -3 1101 0011 1100 1101
the sum 4 0100 0100 N/A N/A

-1+7=6

Number Represented as Binary code One's complement Two's complement
the summand -1 1111 0001 1110 1111
the summand 7 0111 0111 N/A N/A
the sum 6 0110 0110 N/A N/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

Number Binary code
the summand 7 0111
the summand 1 0001
the carries 01110
the result overflow 01000

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

-7+7=0

Number Binary code
the summand -7 1001
the summand 7 0111
the carries 11110
the result 0 10000

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.

URL copied to clipboard
PLANETCALC, One's complement, and two's complement binary codes

Comments