 Open in App
Not now

# Two’s Complement

• Last Updated : 21 Mar, 2023

There are three different ways to represent signed integer (article). a: Signed bit, b: 1’s Complement, and c: 2’s Complement. Let’s try to understand how these methods have derived and why 2’s complement is preferred over others.

As we know that data are stored in bits. How can we store signed integer in the memory? To solve this problem, first we will develop a naïve solution and then will iterate it till we have the best solution for our problem.

## a) Signed bit

When trying to store a signed integer, it seems obvious to reserve the left most bit for sign and use remaining bits to actually store the values. For example: in 4-bit system, first bit from left will be reserved for sign (0 represent positive whereas 1 represent negative) and other 3 bits will be used to store the values. Similarly in 8-bit system, first bit from left will be used for sign and remaining 7 will be used for values.

By using this approach, we are successfully able to represent signed integer. But when we analysis it more closely, we could observe following drawbacks:

### 1) Two representations of zero:

In 4-bit system, we should be able to store 16 (24) values, but +1 to +7 and -1 to -7 are only 14 values. Where are remaining two values? When we observe the table carefully, we will find out that those two values converge to 0. Thus, we have two representations of zero, that means, one representation for +0 and another for -0.

But are two representations of 0 a big concern? So what? Instead of 16 unique values, we are only able to store 15 values. We can afford to reduce the range by 1, isn’t it? To the software developer, it might not concern but for a circuit designer it might be very frustrating to first check if value is +0 and then to check if it -0.

### 2) Signed extension doesn’t work for negative numbers:

Size of the data is increasing rapidly. Some time we need to extend the bit system so that we can increase the range of data that can be stored. In 2014, Gangnam Style video overflowed YouTube view limit and it forced YouTube to upgrade view count from 32-bits to 64-bits signed integer. Similarly, 32-bit Unix clock will overflow on 19 Jan 2038 because it records time in seconds in a 32-bit signed integer.

So, it is equally important that our representation system should be extendable easily which is not possible with this representation system.

### 3) Binary addition doesn’t work:

Let’s try to add two binary numbers:

Why is a simple binary addition not working here? The reason is that the sign bit (left most) is not an ordinary bit and not part of actual number. Imagine the situation where one has to design the hardware circuitry to ignore the sign bit to perform addition and then append the sign bit.

So, this was a naïve way to represent signed integer. The main problem with this approach is that we have mapped negative numbers down up. If we change our mapping system to top down them some of above issue will be resolved.

## b) 1’s Complement

If we remap our negative numbers from top-down, then we will be getting following binary table:

How to get binary representation of an integer in 1’s complement method?

• Positive numbers are represented similar to signed integer method
• Negative numbers are represented by inverting every bit of corresponding positive number (inverting can easily be done by using NOT gate during hardware design)

Let’s analyze this closely to see if we have achieved some improvement.

### 1) Two representations of zero:

In this approach also we have two representations of zero.

### 2) Signed extension doesn’t work for negative numbers:

Signed extension works perfectly for negative numbers.

### 3) Binary addition works with modified rules:

The answer is not always correct, but it is very close to the right answer. We can make it work if we follow the rule that if you have generated carry forward on your left most bit, then don’t throw it away instead bring it back and add it to the right most bit.

Definitely 1’s complement method is better than signed bit. Our major concerns are resolved but remain issue (having two representations of zero) and our hack in binary addition give clues to improve 1’s complement method. Let’s rephrase those sentences to make it easier.

• We have an extra representation of zero which is unnecessary
• While addition two binary numbers, if we have a carry forward in left most bit, then we have to add +1 to the result i.e., the right answer can be found by traversing down to next row in the binary table.

Both of them direct us that an extra representation of zero is the root cause of issue. So, let’s remove this extra zero and shift all negative values to the next row (-7 will move from I -> J, -6 will move from J -> K and so on…)

## c) 2’s Complement

When we remove -0 from the 1’s complement table and shift all negative values one row below, then we will get following table which is called 2’s complement:

How to get binary representation of an integer in 2’s complement method?

• Positive numbers are represented similar to signed integer method
• Negative numbers are represented by inverting every bit of corresponding positive number then adding 1 bit to it

### 1) One representation of zero:

Now we have only one representation of zero and it allows us to store total 16 unique values (+0 to +7 and -1 to -8).

### 2) Signed extension works for negative numbers:

Signed extension works perfectly for negative numbers.

### 4) First bit is a signed bit:

2’s complement has this nice property that first bit is a sign bit because all positive starts with 0 whereas all negative with 1.

### 5) Memory overflow check:

While doing addition, we made sure that our answer is within the range but while designing hardware, memory overflow needs to be detected. It will be very bad idea for hardware designers to check magnitude to catch overflow. 2’s complement method provides a very simple way to detect memory overflow. If carry in to signed bit is not equal to carry out of signed bit, then it is case of memory overflow i.e., if carry in to signed bit is 0 but carry out is 1 or if carry in 1 but carry out is 0, then it is case of memory overflow.

My Personal Notes arrow_drop_up
Related Articles