Exercise 0.1: Binary and decimal: ten conversions
Convert each binary number to decimal, each decimal number to binary, or answer the question.
Method: in binary, the rightmost digit has the weight 1, and each place to the left doubles the weight: 1, 2, 4, 8, 16, 32, 64, 128. A binary number is the sum of the weights under its 1s. To go the other way, subtract the largest weight that fits, write 1 there, and carry on with what is left.
- a) 1011 in decimal
- b) 110010 in decimal
- c) 1111111 (seven 1s) in decimal
- d) 10000000 in decimal
- e) 11101001 in decimal
- f) 19 in binary
- g) 100 in binary
- h) 255 in binary
- i) 64 in binary
- j) The largest whole number that can be written with 6 bits, in decimal
Show the solution
Answers
- a) 11
- b) 50
- c) 127
- d) 128
- e) 233
- f) 10011
- g) 1100100
- h) 11111111
- i) 1000000
- j) 63
Write the weights above the digits, starting with 1 above the rightmost digit, then add the weights that sit above a 1.
Weights 8, 4, 2, 1 above 1, 0, 1, 1: 8 + 2 + 1 = 11.
Pitfall: giving the weight 1 to the leftmost digit, which reads 1101 and gives 13.
Weights 32, 16, 8, 4, 2, 1 above 1, 1, 0, 0, 1, 0: 32 + 16 + 2 = 50.
64 + 32 + 16 + 8 + 4 + 2 + 1 = 127. Seven 1s are one less than the next weight, 128: 2 ** 7 - 1 = 127.
A 1 followed by seven 0s has the weight 2 ** 7 = 128.
Weights 128, 64, 32, 16, 8, 4, 2, 1 above 1, 1, 1, 0, 1, 0, 0, 1: 128 + 64 + 32 + 8 + 1 = 233.
16 fits in 19, leaving 3; 8 and 4 do not fit; 2 fits, leaving 1; 1 fits: a 1 at the weights 16, 2 and 1, so 10011.
Check: 16 + 2 + 1 = 19.
64 fits, leaving 36; 32 fits, leaving 4; 16 and 8 do not fit; 4 fits, leaving 0: 1100100.
Check: 64 + 32 + 4 = 100.
255 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1: eight 1s, 11111111. 255 is 2 ** 8 - 1, the largest value of one byte.
64 = 2 ** 6: a 1 followed by six 0s, 1000000, seven digits.
Six 1s: 32 + 16 + 8 + 4 + 2 + 1 = 63 = 2 ** 6 - 1. 6 bits give 2 ** 6 = 64 values, from 0 to 63.