2.3 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	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.
- 01010101
 
~b
- 00000101
 
~a & ~b
- 
00000001
 - 
10000000
 - 
01010000
 - 
00001010
 - 
01010000
 - 
10101011
 
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.
- 
If
arepresents a positive integer, andone = Bits(1, length=len(a)), give an expression equivalent to-a, but which does not use negation. - 
It is extremely easy to double a binary number: just shift all the bits to the left. (
a << 1is twicea.) Explain why this trick works. - 
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?
- 
What is the bit representation of negative zero? Explain your answer.
 - 
What's the largest integer that can be represented in a single byte? Explain your reasoning.
 - 
What's the smallest integer that can be represented in a single byte? Explain your reasoning.
 - 
What's the largest integer that can be represented in
nbits? Explain your reasoning. 
Text questions
- 
Look at the bits for a few different characters using the
utf8encoding. You will notice they have different bit lengths:>>> Bits('a', encoding='utf8') 01100001 >>> Bits('ñ', encoding='utf8') 1100001110110001 >>> Bits('♣', encoding='utf8') 111000101001100110100011 >>> Bits('😍', encoding='utf8') 11110000100111111001100010001101When 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.