In: Computer Science
Assuming integers are represented as 16-bit words and negative numbers are represented using the 2's complementary method, convert the following hexadecimal numbers to decimal numbers a. 0xCAFE, b. 0x4DAD, c. 0xFACE
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
using this table for converting hexadecimal to binary
a)
0xCAFE
in binary it is 1100 1010 1111 1110
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
1100101011111110 is flipped to 0011010100000001
II. Add 1 to above result
0011010100000001 + 1 = 0011010100000010
III. Now convert this result to decimal value
=> 0011010100000010
=> 0x2^15+0x2^14+1x2^13+1x2^12+0x2^11+1x2^10+0x2^9+1x2^8+0x2^7+0x2^6+0x2^5+0x2^4+0x2^3+0x2^2+1x2^1+0x2^0
=> 0x32768+0x16384+1x8192+1x4096+0x2048+1x1024+0x512+1x256+0x128+0x64+0x32+0x16+0x8+0x4+1x2+0x1
=> 0+0+8192+4096+0+1024+0+256+0+0+0+0+0+0+2+0
=> 13570
Answer: -13570
b)
0x4DAD
in binary it is 0100 1101 1010 1101
since left most bit is 0, this number is positive
so, we can directly convert this into a decimal value
=> 0100110110101101
=> 0x2^15+1x2^14+0x2^13+0x2^12+1x2^11+1x2^10+0x2^9+1x2^8+1x2^7+0x2^6+1x2^5+0x2^4+1x2^3+1x2^2+0x2^1+1x2^0
=> 0x32768+1x16384+0x8192+0x4096+1x2048+1x1024+0x512+1x256+1x128+0x64+1x32+0x16+1x8+1x4+0x2+1x1
=> 0+16384+0+0+2048+1024+0+256+128+0+32+0+8+4+0+1
=> 19885
Answer: 19885
c)
0xFACE
in binary it is 1111 1010 1100 1110
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
1111101011001110 is flipped to 0000010100110001
II. Add 1 to above result
0000010100110001 + 1 = 0000010100110010
III. Now convert this result to decimal value
=> 0000010100110010
=> 0x2^15+0x2^14+0x2^13+0x2^12+0x2^11+1x2^10+0x2^9+1x2^8+0x2^7+0x2^6+1x2^5+1x2^4+0x2^3+0x2^2+1x2^1+0x2^0
=> 0x32768+0x16384+0x8192+0x4096+0x2048+1x1024+0x512+1x256+0x128+0x64+1x32+1x16+0x8+0x4+1x2+0x1
=> 0+0+0+0+0+1024+0+256+0+0+32+16+0+0+2+0
=> 1330
Answer: -1330