# 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 a & ~b 6. 00001010 ~a & b 7. 01010000 a & ~b 8. 10101011 ~b << 1 ## 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. - I am honestly confused what this is asking. I thought it was asking how to make -one without negation but I think instead it's asking for the -a which is the length of the byte? I'm not sure how we could have negative byte length? 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. - Because the each bit-space has the value of 2^n where n is the counted from the right up to where the flip is switched (I am definitely not good at explaining this concept). Therefore moving the flipped switch to the left essentially multiplies the integer-equivalent number by another 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? - Because the addition of 0110100 + 01100100 gives us 11001000 due to the 6th and 7th bit-places carrying over extra 1s. This results in the 8th bit-place to be "1" which is interpreted as "negative". 12. What is the bit representation of negative zero? Explain your answer. - 8-bit representation of 0 would be 00000000 while the negative zero is 10000000 since the 8th bit-space makes the bit number negative. This also can be explained with the negation process which flips the bits and adds one that carries over to the end. 13. What's the largest integer that can be represented in a single byte? Explain your reasoning. - Largest integer would be represented with 11111111. This would have the value of 1+2+4+8+16+32+64+128=255. However for the positive-negative distinction to take place, we can instead say the largest integer would be 01111111 which would have the value 1+2+4+8+16+32+64 = 127. 14. What's the smallest integer that can be represented in a single byte? Explain your reasoning. - Similar to the first answer above, it would be 10000000. Since "negative zero" isn't really a thing (we dont sign zero), the smallest integer would be -128 with the 8th bit-space making the 2^7 negative. 15. What's the largest integer that can be represented in `n` bits? Explain your reasoning. - I think based on my answer to 13th question, it would be (2^(n - 1)) - 1, if the integers have signs. Without the sign systems, it would be (2^n) - 1. ## 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. - I think it coudl be some sort of an "if" statement involved in the encoding procedure. If there are more bits to be read, the encoding continues on and finalizes what character is represented. I also assume the length originates because we keep on adding more characters into our digital "encoding" registry. Emojis weren't as common maybe 10 years while the spade might have been. Over time, the way we communicate evolves, so does the library of tools and means for our communication. I would assume Unicode or utf-8 is updated based on the new trends in our languages and communications. Which US should implement.