diff --git a/number-systems/Part-1.md b/number-systems/Part-1.md index d8f9c290e..264ebc8d6 100644 --- a/number-systems/Part-1.md +++ b/number-systems/Part-1.md @@ -7,48 +7,74 @@ The goal of these exercises is for you to gain an intuition for binary numbers. The answers to these questions should be a number, either in binary, hex, or decimal. Q1: Convert the decimal number 14 to binary. -Answer: +Answer: 1110 Q2: Convert the binary number 101101 to decimal: -Answer: +Answer: 45 Q3: Which is larger: 1000 or 0111? -Answer: +Answer: 1000 + +I compared the bits from the left. The first bit is already different: 1000 has a 1 there and 0111 has a 0, so 1000 is bigger. Q4: Which is larger: 00100 or 01011? -Answer: +Answer: 01011 + +Both numbers start with 0. The next digit is 1 in 01011 and 0 in 00100, so 01011 is bigger. Q5: What is 10101 + 01010? -Answer: +Answer: 11111 + +Each column has one 1 and one 0, so every column gives 1 and there are no carries. Q6: What is 10001 + 10001? -Answer: +Answer: 100010 + +The rightmost 1 + 1 gives 0 and carries 1 into the next column. The two leftmost 1s also give 0 and carry 1 into a new column. Q7: What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? -Answer: +Answer: 15 + +Four bits give 2 x 2 x 2 x 2 = 16 possible patterns. Counting from 0, the last value is 15, represented by 1111. Q8: How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: +Answer: 8 + +Eight bits give 256 different patterns, enough for every number from 0 through 255. Q9: How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: +Answer: 2 + +Two bits give four patterns: 00, 01, 10, and 11. Q10: How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: +Answer: 10 + +Nine bits give 512 patterns. Ten bits give 1024, enough for all 1001 values from 0 through 1000. Q11: Convert the decimal number 14 to hex. -Answer: +Answer: E + +Hex uses A through F for the values 10 through 15, so 14 is E. Q12: Convert the decimal number 386 to hex. -Answer: +Answer: 182 + +386 is one group of 256, eight groups of 16, and two ones. Those are the three hex digits. Q13: Convert the hex number 386 to decimal. -Answer: +Answer: 902 + +The digits are worth 3 × 256, 8 × 16, and 6 × 1. Adding 768, 128, and 6 gives 902. Q14: Convert the hex number B to decimal. -Answer: +Answer: 11 + +In hex, A is 10 and B is the next digit, so B is 11. Q15: If reading the byte 0x21 as a number, what decimal number would it mean? -Answer: +Answer: 33 + +The 2 is in the sixteens place and the 1 is in the ones place. That gives 32 + 1 = 33. Q16: Continues in Part-2 diff --git a/number-systems/Part-2.md b/number-systems/Part-2.md index 68b0933d9..95b566e52 100644 --- a/number-systems/Part-2.md +++ b/number-systems/Part-2.md @@ -7,16 +7,24 @@ The goal of these exercises is for you to gain an intuition for binary numbers. The answers to these questions will require a bit of explanation, not just a simple answer. Q16: How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: +Answer: I just count the `1`s. If there’s exactly one, it’s a power of two. `1000` has one `1`, so it counts. `1010` has two, so it doesn’t... Q17: If reading the byte 0x21 as an ASCII character, what character would it mean? -Answer: +Answer: An exclamation mark (!). + +The hex value 0x21 is 33 in decimal and ASCII is a table that assigns a character to each code number, and code 33 is the exclamation mark. Q18: If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer: Probably a very dark grey. + +In 8-bit greyscale, 0 means black and 255 means white. The value 0x21 is 33, which is much closer to 0 than to 255. That puts it near the black end of the scale... Q19: If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? -Answer: +Answer: 170, 0, and 255. + +Each pair of hex digits is one byte, so split 0xAA00FF into AA, 00, and FF. A is 10, so AA is 10 x 16 + 10 = 170. The middle byte is 0. F is 15, so FF is 15 x 16 + 15 = 255. Q20: If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer: A nice vivid purple. + +RGB reads the three bytes in red, green, blue order. That gives red 170, green 0, and blue 255. Red and blue together make purple, and the stronger blue gives it a bluish tint.