In: Computer Science
1) Number: 47 Let's convert this to two's complement binary Since this is a positive number. we can directly convert this into binary Divide 47 successively by 2 until the quotient is 0 > 47/2 = 23, remainder is 1 > 23/2 = 11, remainder is 1 > 11/2 = 5, remainder is 1 > 5/2 = 2, remainder is 1 > 2/2 = 1, remainder is 0 > 1/2 = 0, remainder is 1 Read remainders from the bottom to top as 101111 So, 47 of decimal is 101111 in binary Adding 2 zeros on left hand side of this number to make this of length 8 so, 47 in 2's complement binary is 00101111 Number: -38 Let's convert this to two's complement binary This is negative. so, follow these steps to convert this into a 2's complement binary Step 1: Divide 38 successively by 2 until the quotient is 0 > 38/2 = 19, remainder is 0 > 19/2 = 9, remainder is 1 > 9/2 = 4, remainder is 1 > 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 100110 So, 38 of decimal is 100110 in binary Adding 2 zeros on left hand side of this number to make this of length 8 So, 38 in normal binary is 00100110 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 00100110 is flipped to 11011001 Step 3:. Add 1 to above result 11011001 + 1 = 11011010 so, -38 in 2's complement binary is 11011010 Adding 00101111 and 11011010 in binary 00101111 11011010 ------------- (1)00001001 ------------- Sum produces a carry of 1. We can ignore that carry. So, sum of these numbers in binary is 00001001 Verification: --------------- sum = 00001001 since left most bit is 0, this number is positive so, we can directly convert this into a decimal value => 1001 => 1x2^3+0x2^2+0x2^1+1x2^0 => 1x8+0x4+0x2+1x1 => 8+0+0+1 => 9 Answer: 9 This is correct since we can verify that 47+-38 = 9 So, there was no overflow. 2) Number: -47 Let's convert this to two's complement binary This is negative. so, follow these steps to convert this into a 2's complement binary Step 1: Divide 47 successively by 2 until the quotient is 0 > 47/2 = 23, remainder is 1 > 23/2 = 11, remainder is 1 > 11/2 = 5, remainder is 1 > 5/2 = 2, remainder is 1 > 2/2 = 1, remainder is 0 > 1/2 = 0, remainder is 1 Read remainders from the bottom to top as 101111 So, 47 of decimal is 101111 in binary Adding 2 zeros on left hand side of this number to make this of length 8 So, 47 in normal binary is 00101111 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 00101111 is flipped to 11010000 Step 3:. Add 1 to above result 11010000 + 1 = 11010001 so, -47 in 2's complement binary is 11010001 Number: -38 Let's convert this to two's complement binary This is negative. so, follow these steps to convert this into a 2's complement binary Step 1: Divide 38 successively by 2 until the quotient is 0 > 38/2 = 19, remainder is 0 > 19/2 = 9, remainder is 1 > 9/2 = 4, remainder is 1 > 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 100110 So, 38 of decimal is 100110 in binary Adding 2 zeros on left hand side of this number to make this of length 8 So, 38 in normal binary is 00100110 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 00100110 is flipped to 11011001 Step 3:. Add 1 to above result 11011001 + 1 = 11011010 so, -38 in 2's complement binary is 11011010 Adding 11010001 and 11011010 in binary 11010001 11011010 ------------- (1)10101011 ------------- Sum produces a carry of 1. We can ignore that carry. So, sum of these numbers in binary is 10101011 Verification: --------------- sum = 10101011 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. 10101011 is flipped to 01010100 II. Add 1 to above result 01010100 + 1 = 01010101 III. Now convert this result to decimal value => 1010101 => 1x2^6+0x2^5+1x2^4+0x2^3+1x2^2+0x2^1+1x2^0 => 1x64+0x32+1x16+0x8+1x4+0x2+1x1 => 64+0+16+0+4+0+1 => 85 Answer: -85 This is correct since we can verify that -47+-38 = -85 So, there was no overflow.