In: Computer Science
Number: 18 Let's convert this to two's complement binary 18 Since this is a positive number. we can directly convert this into binary Divide 18 successively by 2 until the quotient is 0 > 18/2 = 9, remainder is 0 > 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 10010 So, 18 of decimal is 10010 in binary so, 18 in 2's complement binary is 010010 Number: -10 Let's convert this to two's complement binary -10 This is negative. so, follow these steps to convert this into a 2's complement binary Step 1: Divide 10 successively by 2 until the quotient is 0 > 10/2 = 5, remainder is 0 > 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 1010 So, 10 of decimal is 1010 in binary So, 10 in normal binary is 001010 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 001010 is flipped to 110101 Step 3:. Add 1 to above result 110101 + 1 = 110110 so, -10 in 2's complement binary is 110110 Adding 010010 and 110110 in binary 010010 110110 ----------- (1)001000 ----------- Sum produces a carry of 1. We can ignore that carry. So, sum of these numbers in binary is 001000 Verification: --------------- sum = 001000 since left most bit is 0, this number is positive so, we can directly convert this into a 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 This is correct since we can verify that 18+-10 = 8 So, there was no overflow.