Open In App

Arithmetic Operations of 2’s Complement Number System

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We all know how to perform arithmetic operations of binary number systems. However, computer system generally stores numbers in 2’s complement format. So it becomes necessary for us to know how arithmetic operations are done in 2’s complement.

Addition:

In 2’s complement, we perform addition the same as an addition for unsigned binary numbers. The only difference is here we discard the out carry i.e carry from MSB bit as long as the range lies within the accepted range for 2’s complement representation.

For eg: Consider a number of bits(n) = 4. So range of numbers will be -2^{n-1} \space to \space 2^{n-1} , i.e. -8 to 7.

x=2, y=3        
addition: 0010
          0011
          0101 
---> +5 in 4 bits
x=-2,y=-3     
addition: 1110
          1101
          1011 
---> -5 in 4 bits 
(discarding the out carry 1)

In the above examples, there was no overflow as the answers +5 and -5 lie in the range of -8 to 7.

Consider the example,

x=4, y=5        
addition: 0100
          0101
          1001 
---> -7 (not in range of -8 to 7)

Hence overflow occurs in the above example as -7 does not exist in the 4bits range.

For more details on overflow, you can refer to the following article Overflow in Arithmetic Addition in Binary Number System. 

Subtraction

Consider if we have to find the subtraction x-y, we perform the following steps:

  1. Take 2’s complement of y
  2. Add x and 2’s complement of y

for eg:

x=3, y=2 for 4 bits 2's complement representation

3---> 0011
Take 2's complement of 2----> 0010
1101 + 1 ---> 1110

Adding 0011 and 1110
0011
1110
0001 

---> +1 (discard carry 1 from MSB)

Multiplication:

If operands x and y which need to be multiplied are of n and m digits respectively then the result will be n+m digits. To get correct results we extend digits of both operands to n+m digits

For eg: x=13, y=-6

13 requires 5(m) bits for representation. and -6 requires 4 bits(n) so we will make the bits of 13 and -6 to be equal to m+n = 5+4 = 9

13 ------> 000001101
-6 ------> 111111010 
(2's complement of 6)

We then take 2’s complement of 110110010 which is 001001110  which is 78. And so 110110010 is -78.

Conclusion:

In this article, we have seen addition, subtraction, and multiplication in 2’s complement representation of signed integers. If you want to read more about different types of representations of signed integers and why 2’s complement is the best among all do read this article Different ways to represent Signed Integers


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads