diff --git a/questions.md b/questions.md index 6c781ce..01290bc 100644 --- a/questions.md +++ b/questions.md @@ -52,9 +52,13 @@ talk with others. 9. If `a` represents a positive integer, and `one = Bits(1, length=len(a))`, give an expression equivalent to `-a`, but which does not use negation. +~a + Bits(1, length = len(a)) + 10. It is extremely easy to double a binary number: just shift all the bits to the left. (`a << 1` is twice `a`.) Explain why this trick works. +From a math approach, binary is writing numbers in base 2, so each 0/1 represents a power of 2, increasing as you go to the left. For example, 00000101 = 5, beacuse its 2^2 + 2^0 = 4 + 1 = 5. So every time you shift the binary numbers once to the left, you are introducing a new power of 2, or effectively multiplying by 2. + 11. Consider the following: ``` >>> hundred = Bits(100, 8) @@ -67,17 +71,27 @@ talk with others. ``` Apparently 100 + 100 = -56. What's going on here? +Since we are only using 8 bits, the highest possible positive number we can represent is 127 (just shy of 2^7, which is 128, because we can only represent up to 2^7 with 8 digits). When you do the bitwise addition, you end up with a 1 as the first bit, which signals to the computer that this is a negative number. In other words, you can't do proper integer addition in 8 bits with a positive result higher than 127, because it cannot be represented in 8 bits. + 12. What is the bit representation of negative zero? Explain your answer. +I was a bit hesitant about this one, because my math brain tells me that there is no such thing as negative 0, so I tried Bits(0,8) and Bits(-0,8) and turns out I was right, they are both 00000000. + 13. What's the largest integer that can be represented in a single byte? Explain your reasoning. + Oh I already did this one! It's 127, see my reasoning above. + 14. What's the smallest integer that can be represented in a single byte? Explain your reasoning. + -128, similarly to above. This is represented by 10000000, which is effectively the inverse of 127, but minus 1 (which are the steps for finding the negative) + 15. What's the largest integer that can be represented in `n` bits? Explain your reasoning. +(2^(n-1))-1. Similar reasoning to how I got 127, because it will be one less than the highest power of 2 made from the given powers. Yay math questions! + ## Text questions 16. Look at the bits for a few different characters using the `utf8` encoding.