In: Computer Science
2) Show how each of the following signed, decimal integers would be stored in 16-bit two's complement format. Give your answer in hexadecimal.
a) -21 (5 points)
b) 4096 (5 points)
a)
This is negative. so, follow these steps to convert this into a 2's complement binary
Step 1:
Divide 21 successively by 2 until the quotient is 0
> 21/2 = 10, remainder is 1
> 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 10101
So, 21 of decimal is 10101 in binary
Adding 11 zeros on left hand side of this number to make this of length 16
So, 21 in normal binary is 0000000000010101
Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0.
0000000000010101 is flipped to 1111111111101010
Step 3:. Add 1 to above result
1111111111101010 + 1 = 1111111111101011
so, -21 in 2's complement binary is 1111111111101011
Let's convert this to hexadecimal
Hexadecimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Use this table to convert from binary to hexadecimal
Converting 1111111111101011 to hexadecimal
1111 => F
1111 => F
1110 => E
1011 => B
So, in hexadecimal 1111111111101011 is 0xFFEB
Answer: 0xFFEB
b)
Since this is a positive number. we can directly convert this into binary
Divide 4096 successively by 2 until the quotient is 0
> 4096/2 = 2048, remainder is 0
> 2048/2 = 1024, remainder is 0
> 1024/2 = 512, remainder is 0
> 512/2 = 256, remainder 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 1000000000000
So, 4096 of decimal is 1000000000000 in binary
Adding 3 zeros on left hand side of this number to make this of length 16
so, 4096 in 2's complement binary is 0001000000000000
Let's convert this to hexadecimal
Hexadecimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Use this table to convert from binary to hexadecimal
Converting 0001000000000000 to hexadecimal
0001 => 1
0000 => 0
0000 => 0
0000 => 0
So, in hexadecimal 0001000000000000 is 0x1000
Answer: 0x1000