# Boolean questions Create the following variables. ``` a = Bits("11110000") b = Bits("10101010") ``` For each of the following bytes, give an equivalent expression which uses only `a`, `b`, and bit operators. The answers to the first two questions are given. 1. 01010101 ~b 2. 00000101 ~a & ~b 3. 00000001 ~a >> 3 4. 10000000 a << 3 5. 01010000 ~b & a 6. 00001010 ~a & b 7. 01010000 ~b << 4 8. 10101011 ~a >> 3 | b ## Integer questions These questions are difficult! Try exploring ideas with `Bits` in Terminal, a paper and pencil, and a whiteboard. And definitely 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=91 >>>a 01011011 >>>one=Bits(1, 8) >>>one 00000001 >>>~a 10100100 **>>>~a + one** 10100101 >>>-a 10100101 >>>-a.int -91 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. Every binary digit place, from right to left, each left shift of one, raises the digit to the power of 2. 11. Consider the following: ``` >>> hundred = Bits(100, 8) >>> hundred 01100100 >>> (hundred + hundred) 11001000 >>> (hundred + hundred).int -56 ``` Apparently 100 + 100 = -56. What's going on here? The range of Bits in this case is from -128 to 128, in base 10. If you change the first line to: **hundred = Bits(100, 9)**, that ninth bit (on the left) increases the Bits range to from -256 to 256, in base 10. So, then you get the right answer: 200. 12. What is the bit representation of negative zero? Explain your answer. "What is negative 0 in binary? In a 1+7-bit sign-and-magnitude representation for integers, negative zero is represented by the bit string 1000 0000 . In an 8-bit ones' complement representation, negative zero is represented by the bit string 1111 1111 . In all these three encodings, positive or unsigned zero is represented by 0000 0000 ." - Google Search 13. What's the largest integer that can be represented in a single byte? (8 bits in a byte). Explain your reasoning. 127 "For a signed integer (the most common representation in modern computing, using two's complement), the range is -128 to 127." - Google Search 14. What's the smallest integer that can be represented in a single byte? (8 bits in a byte). Explain your reasoning. -128 "For a signed integer (the most common representation in modern computing, using two's complement), the range is -128 to 127." - Google Search 15. What's the largest integer that can be represented in `n` bits? ((2**n)/2) - 1. Explain your reasoning. Again, power of 2 for each bit, as you go to the left, one bit at a time. Also, the range of integers must include negative numbers, so you divide the total by 2. Then you subtract 1 because 0 is included in the range. ## Text questions 16. Look at the bits for a few different characters using the `utf8` encoding. You will notice they have different bit lengths: ``` >>> Bits('a', encoding='utf8') 01100001 >>> Bits('ñ', encoding='utf8') 1100001110110001 >>> Bits('♣', encoding='utf8') 111000101001100110100011 >>> Bits('😍', encoding='utf8') 11110000100111111001100010001101 ``` When it's time to decode a sequence of utf8-encoded bits, the decoder somehow needs to decide when it has read enough bits to decode a character, and when it needs to keep reading. For example, the decoder will produce 'a' after reading 8 bits but after reading the first 8 bits of 'ñ', the decoder realizes it needs to read 8 more bits. Make a hypothesis about how this could work.