In: Computer Science
convert +38 and +17 to binary using the signed 2s complement representation and enough digits to accomaodate the numbers. Then perform the binary equivalent of (-38) and +17
Calculating 38 in binary:
--------------------------
Write down the decimal number and continually divide by 2 to give a result and a remainder. The remainder is either a 1 or a 0.
38 / 2 result 19 remainder 0
19 / 2 result 9 remainder 1
9 / 2 result 4 remainder 1
4 / 2 result 2 remainder 0
2 / 2 result 1 remainder 0
1 / 2 result 0 remainder 1
Read the remainders from bottom to top.
( 38 )10 = ( 100110 )2
So, fill zeros to the front to make it 8 bit
( 38 )10 = ( 00100110 )2
Ones complement = Inverting all the bits in binary
= 11011001
Twos complement = Ones complement + 1
= 11011001 + 1
= 11011010
So, -38 in binary is 11011010
=====================================
Calculating 17 in binary:
--------------------------
Write down the decimal number and continually divide by 2 to give a result and a remainder. The remainder is either a 1 or a 0.
17 / 2 result 8 remainder 1
8 / 2 result 4 remainder 0
4 / 2 result 2 remainder 0
2 / 2 result 1 remainder 0
1 / 2 result 0 remainder 1
Read the remainders from bottom to top.
( 17 )10 = ( 10001 )2
So, fill zeros to the front to make it 8 bit
( 17 )10 = ( 00010001 )2
So, 17 in binary is 00010001
=====================================
Calculating -38+17 =
-------------------------
11011010
+ 00010001
------------
11101011
------------
So, the result of -38+17 in binary is 11101011
we have to convert 11101011 back to decimal to make answer in decimal
As MSB is 1, this is in two's complement
Ones complement = Twos complement - 1
= 11101011 - 1
= 11101010
Binary = -Inverting all the bits in binary
= -00010101
So, The decimal value of 00010101 is 21
So, 11101011 in decimal is -21
Final Answer in binary is 11101011
Final Answer in decimal is -21