1000. Convert the following decimal numbers to binary (assume a 16 bit Integer: 1 bit for the sign, 15 bits for the value). If necessary, assume two’s complement.
a. 100 d. ‑87
b. 978 e. ‑5,345
c. 32,767 f. -32,789
1.a. 100 DIV 2 = 50 100 MOD 2 = 0 10010 = 11001002
50 DIV 2 = 25 50 MOD 2 = 0
25 DIV 2 = 12 25 MOD 2 = 1 Chk: 26 + 25 + 22 = 64 + 32 + 4
12 DIV 2 = 6 12 MOD 2 = 0 = 100
6 DIV 2 = 3 6 MOD 2 = 0
3 DIV 2 = 1 3 MOD 2 = 1 Ans: 0,000000001100100
1 DIV 2 = 0 1 MOD 2 = 1
b. 978 DIV 2 = 489 978 MOD 2 = 0 97810 = 11110100102
489 DIV 2 = 244 489 MOD 2 = 1
244 DIV 2 = 122 244 MOD 2 = 0 Chk: 29 + 28 + 27 + 26 + 24 + 21
122 DIV 2 = 61 122 MOD 2 = 0 = 512+256+128+64+16+2
61 DIV 2 = 30 61 MOD 2 = 1 = 978
30 DIV 2 = 15 30 MOD 2 = 0
15 DIV 2 = 7 15 MOD 2 = 1
7 DIV 2 = 3 7 MOD 2 = 1 Ans: 0,000001111010010
3 DIV 2 = 1 3 MOD 2 = 1
1 DIV 2 = 0 1 MOD 2 = 1
c. 32,767 DIV 2 = 16,383 32,767 MOD 2 = 1 32,76710 = 1111111111111112
16,383 DIV 2 = 8,191 16,383 MOD 2 = 1
8,191 DIV 2 = 4,095 8,191 MOD 2 = 1 Chk: 214+213+212+211+210+29 +28
4,095 DIV 2 = 2,047 4,095 MOD 2 = 1 +27+26+25+24+23 +22+21+20
2,047 DIV 2 = 1,023 2,047 MOD 2 = 1 = 16,384 + 8,192 + 4,096 +
1,023 DIV 2 = 511 1,023 MOD 2 = 1 2,048 + 1,024 + 512 + 256 +
511 DIV 2 = 255 511 MOD 2 = 1 128 + 64 + 32 + 16 + 8 + 4 +
255 DIV 2 = 127 255 MOD 2 = 1 2 + 1
127 DIV 2 = 63 127 MOD 2 = 1 = 32,767
63 DIV 2 = 31 63 MOD 2 = 1
31 DIV 2 = 15 31 MOD 2 = 1 Which we could have predicted
15 DIV 2 = 7 15 MOD 2 = 1
7 DIV 2 = 3 7 MOD 2 = 1
3 DIV 2 = 1 3 MOD 2 = 1 Ans: 0,111111111111111
1 DIV 2 = 0 1 MOD 2 = 1
d. 87 DIV 2 = 43 87 MOD 2 = 1 8710 = 10101112
43 DIV 2 = 21 43 MOD 2 = 1
21 DIV 2 = 10 21 MOD 2 = 1 Chk: 26 + 24 + 22 + 21 + 20
10 DIV 2 = 5 10 MOD 2 = 0 = 64 + 16 + 4 + 2 + 1
5 DIV 2 = 2 5 MOD 2 = 1 = 87
2 DIV 2 = 1 2 MOD 2 = 0
1 DIV 2 = 0 1 MOD 2 = 1
BUT we need to complement:
0,000000001010111 Ţ 1,111111110101000 (One’s Complement)
+ 1 Ans.
1,111111110101001 (Two’s Complement)
e. 5,344 DIV 2 = 2,672 5,345 MOD 2 = 0 5,34510 = 10100111000002
2,672 DIV 2 = 1,336 2,672 MOD 2 = 0
1,336 DIV 2 = 668 1,336 MOD 2 = 0 Chk: 212+210 +27+26+2520
668 DIV 2 = 334 668 MOD 2 = 0 = 4,096 + 1,024 + 128 + 64 + 32
334 DIV 2 = 167 334 MOD 2 = 0 = 5,344
167 DIV 2 = 83 167 MOD 2 = 1
83 DIV 2 = 41 83 MOD 2 = 1
41 DIV 2 = 20 41 MOD 2 = 1
20 DIV 2 = 10 20 MOD 2 = 0 BUT (again) we need to
10 DIV 2 = 5 10 MOD 2 = 0 complement
5 DIV 2 = 2 5 MOD 2 = 1
2 DIV 2 = 1 2 MOD 2 = 0
1 DIV 2 = 0 1 MOD 2 = 1
11111
0,001010011100000 => 1,110101100011111 (One’s Complement)
+ 1 Ans.
1,111010110100000 (Two’s Complement)
f. We Can’t because log(32,789)/log(3) = 4.516/0.30103 = 15.002 = 16
and we only have 15-bits on which to store the integer value. However, we could store the value as a signed long (if it were positive, we could also have stored it as an unsigned int) using 32-bits (1-bit for the sign and 31-bits for the value):
32,789 DIV 2 = 16,394 32,789 MOD 2 = 1 32,76710 = 1000000000101012
16,394 DIV 2 = 8,197 16,394 MOD 2 = 0
8,197 DIV 2 = 4,098 8,197 MOD 2 = 1 Chk: 215 + 24 + 22 +20
4,098 DIV 2 = 2,049 4,098 MOD 2 = 0 = 32,768 + 16 + 4 + 1
2,049 DIV 2 = 1,024 2,049 MOD 2 = 1 = 32,789
1,024 DIV 2 = 512 1,024 MOD 2 = 0
512 DIV 2 = 256 512 MOD 2 = 0
256 DIV 2 = 128 256 MOD 2 = 0
128DIV 2 = 64 128 MOD 2 = 0 BUT (of course) we still must
64 DIV 2 = 32 64 MOD 2 = 0 complement
32 DIV 2 = 16 32 MOD 2 = 0
16 DIV 2 = 8 16 MOD 2 = 0
8 DIV 2 = 4 8 MOD 2 = 0
4 DIV 2 = 2 4 MOD 2 = 0
2 DIV 2 = 1 2 MOD 2 = 0
1 DIV 2 = 0 1 MOD 2 = 1
0, 0000000000000000100000000010101=> 1,1111111111111111011111111101010 (1’s Complement)
+ 1
1,1111111111111111011111111101011 (2’s Complement)