In: Computer Science
Assume that a mad scientist has created a computer that has 9 bit registers. The most significant bit is the sign bit. He wants to execute the following operation using 9 bit register.
-256-2
Use 2's complement method (in binary) to find the result of the above operation in binary system. Show the computations in the answer.
Binary: 011111110 Decimal: 254 overflow happened. Explanation: ------------- Number: -256 Let's convert this to two's complement binary -256 This is negative. so, follow these steps to convert this into a 2's complement binary Step 1: Divide 256 successively by 2 until the quotient is 0 > 256/2 = 128, remainder is 0 > 128/2 = 64, remainder is 0 > 64/2 = 32, remainder is 0 > 32/2 = 16, remainder is 0 > 16/2 = 8, remainder is 0 > 8/2 = 4, remainder 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 100000000 So, 256 of decimal is 100000000 in binary So, 256 in normal binary is 100000000 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 100000000 is flipped to 011111111 Step 3:. Add 1 to above result 011111111 + 1 = 100000000 so, -256 in 2's complement binary is 100000000 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 000000010 Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0. 000000010 is flipped to 111111101 Step 3:. Add 1 to above result 111111101 + 1 = 111111110 so, -2 in 2's complement binary is 111111110 Adding 100000000 and 111111110 in binary 100000000 111111110 -------------- (1)011111110 -------------- Sum produces a carry of 1. We can ignore that carry. So, sum of these numbers in binary is 011111110 Verification: --------------- sum = 011111110 since left most bit is 0, this number is positive so, we can directly convert this into a decimal value Converting 11111110 to decimal 11111110 => 1x2^7+1x2^6+1x2^5+1x2^4+1x2^3+1x2^2+1x2^1+0x2^0 => 1x128+1x64+1x32+1x16+1x8+1x4+1x2+0x1 => 128+64+32+16+8+4+2+0 => 254 Answer: 254 -256+-2 must be -258 This is not correct since we can verify that -256+-2 not equals 254 So, there was an overflow.