In: Computer Science
7 – For the following operations:
• write the operands as 4-bit 2's complement binary numbers,
• perform the operation shown,
• show all work in binary operating on 4-bit numbers, and
• identify overflow if necessary.
a) 4 + 2
b) 4 – 2
c) 2 – 4
d) 4 + 4
a)
Number: 4
Let's convert this to two's complement binary
4
Since this is a positive number. we can directly convert this into binary
Divide 4 successively by 2 until the quotient is 0
   > 4/2 = 2, remainder is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 100
So, 4 of decimal is 100 in binary
so, 4 in 2's complement binary is 0100
Number: 2
Let's convert this to two's complement binary
2
Since this is a positive number. we can directly convert this into binary
Divide 2 successively by 2 until the quotient is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 10
So, 2 of decimal is 10 in binary
so, 2 in 2's complement binary is 0010
Adding 0100 and 0010 in binary
    0100
    0010
---------
 (0)0110
---------
Sum does not produces a carry
So, sum of these numbers in binary is 0110
Verification:
---------------
sum = 0110
since left most bit is 0, this number is positive
so, we can directly convert this into a decimal value
Converting 110 to decimal
110
=> 1x2^2+1x2^1+0x2^0
=> 1x4+1x2+0x1
=> 4+2+0
=> 6
Answer: 6
This is correct since we can verify that 4+2 = 6
So, there was no overflow.
b)
Number: 4
Let's convert this to two's complement binary
4
Since this is a positive number. we can directly convert this into binary
Divide 4 successively by 2 until the quotient is 0
   > 4/2 = 2, remainder is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 100
So, 4 of decimal is 100 in binary
so, 4 in 2's complement binary is 0100
Number: -2
Let's convert this to two's complement binary
-2
This is negative. so, follow these steps to convert this into a 2's complement binary
Step 1:
Divide 2 successively by 2 until the quotient is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 10
So, 2 of decimal is 10 in binary
So, 2 in normal binary is 0010
Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0.
   0010 is flipped to 1101
Step 3:. Add 1 to above result
1101 + 1 = 1110
so, -2 in 2's complement binary is 1110
Adding 0100 and 1110 in binary
    0100
    1110
---------
 (1)0010
---------
Sum produces a carry of 1. We can ignore that carry.
So, sum of these numbers in binary is 0010
Verification:
---------------
sum = 0010
since left most bit is 0, this number is positive
so, we can directly convert this into a decimal value
Converting 10 to decimal
10
=> 1x2^1+0x2^0
=> 1x2+0x1
=> 2+0
=> 2
Answer: 2
This is correct since we can verify that 4+-2 = 2
So, there was no overflow.
c)
Number: 2
Let's convert this to two's complement binary
2
Since this is a positive number. we can directly convert this into binary
Divide 2 successively by 2 until the quotient is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 10
So, 2 of decimal is 10 in binary
so, 2 in 2's complement binary is 0010
Number: -4
Let's convert this to two's complement binary
-4
This is negative. so, follow these steps to convert this into a 2's complement binary
Step 1:
Divide 4 successively by 2 until the quotient is 0
   > 4/2 = 2, remainder is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 100
So, 4 of decimal is 100 in binary
So, 4 in normal binary is 0100
Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0.
   0100 is flipped to 1011
Step 3:. Add 1 to above result
1011 + 1 = 1100
so, -4 in 2's complement binary is 1100
Adding 0010 and 1100 in binary
    0010
    1100
---------
 (0)1110
---------
Sum does not produces a carry
So, sum of these numbers in binary is 1110
Verification:
---------------
sum = 1110
since left most bit is 1, this number is negative number.
so, follow these steps below to convert this into a decimal value.
I. first flip all the bits. Flip all 0's to 1 and all 1's to 0.
   1110 is flipped to 0001
II. Add 1 to above result
0001 + 1 = 0010
III. Now convert this result to decimal value
Converting 10 to decimal
10
=> 1x2^1+0x2^0
=> 1x2+0x1
=> 2+0
=> 2
Answer: -2
This is correct since we can verify that 2+-4 = -2
So, there was no overflow.
d)
Number: 4
Let's convert this to two's complement binary
4
Since this is a positive number. we can directly convert this into binary
Divide 4 successively by 2 until the quotient is 0
   > 4/2 = 2, remainder is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 100
So, 4 of decimal is 100 in binary
so, 4 in 2's complement binary is 0100
Number: 4
Let's convert this to two's complement binary
4
Since this is a positive number. we can directly convert this into binary
Divide 4 successively by 2 until the quotient is 0
   > 4/2 = 2, remainder is 0
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 100
So, 4 of decimal is 100 in binary
so, 4 in 2's complement binary is 0100
Adding 0100 and 0100 in binary
    0100
    0100
---------
 (0)1000
---------
Sum does not produces a carry
So, sum of these numbers in binary is 1000
Verification:
---------------
sum = 1000
since left most bit is 1, this number is negative number.
so, follow these steps below to convert this into a decimal value.
I. first flip all the bits. Flip all 0's to 1 and all 1's to 0.
   1000 is flipped to 0111
II. Add 1 to above result
0111 + 1 = 1000
III. Now convert this result to decimal value
Converting 1000 to decimal
1000
=> 1x2^3+0x2^2+0x2^1+0x2^0
=> 1x8+0x4+0x2+0x1
=> 8+0+0+0
=> 8
Answer: -8
4+4 must be 8
This is not correct since we can verify that 4+4 not equals -8
So, there was an overflow.