@@ -5,43 +5,60 @@ Do not convert any binary numbers to decimal when solving a question unless the
55The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point.
66
77Convert the decimal number 14 to binary.
8- Answer:
8+ Answer: 1000 + 0100 + 0010 = 1110 = 8 + 4 + 2 = 14
99
1010Convert the binary number 101101 to decimal:
11- Answer:
11+ Answer: 2^5 + 2^3 + 2^2 + 2^0 = 32 + 8 + 4 + 1 = 45
1212
1313Which is larger: 1000 or 0111?
14- Answer:
14+ Answer: 1000 because 1000 is 8 and 0111 is 7
1515
1616Which is larger: 00100 or 01011?
17- Answer:
17+ Answer: 01011 because 01011 is 11 and 00100 is 4
1818
1919What is 10101 + 01010?
20- Answer:
20+ Answer: 11111
2121
2222What is 10001 + 10001?
23- Answer:
23+ Answer: 100010
2424
2525What's the largest number you can store with 4 bits, if you want to be able to represent the number 0?
26- Answer:
26+ Answer: 1111
2727
2828How many bits would you need in order to store the numbers between 0 and 255 inclusive?
29- Answer:
29+ Answer: 2^8 = 256. So, we need 7 bits. b0000000 = 0, and b1111111 = 255
3030
3131How many bits would you need in order to store the numbers between 0 and 3 inclusive?
32- Answer:
32+ Answer: 2 bits as 00 = 0, and 11 = 3
3333
3434How many bits would you need in order to store the numbers between 0 and 1000 inclusive?
35- Answer:
35+ Answer: 2^9 = 512
36+ 2^8 = 256
37+ 2^7 = 128
38+ 2^6 = 64
39+ 2^3 = 8
40+ 512 + 256 + 128 + 64 + 32 + 8 = 1000
41+ b1111101000 = 1000
42+ we need 10 bits
3643
3744How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)?
38- Answer:
45+ Answer: 1, 10, 100, 1000, 10000
46+ 1, 2, 4, 8, 16
47+ If the binary starts with 1 or 1 followed by 0s, it will be a power of two.
48+ We can use regexp to test the binary number.
49+
50+ function testPowerOfTwo(binaryNumber) {
51+ return /^0*10*$/.test(String(binaryNumber))
52+ }
3953
4054Convert the decimal number 14 to hex.
41- Answer:
55+ Answer: 10 = A, 11 = B, 12 = C, 13 = D, 14 = E
4256
4357Convert the decimal number 386 to hex.
44- Answer:
58+ Answer: F = 15,
59+ 386 / 15 = 25.7333
60+ 386 - (F * 25) = 11
61+ So, 386 = F* 25+B
4562
4663Convert the hex number 386 to decimal.
4764Answer:
0 commit comments