4.2 KiB
Nelson Mason
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
~a >> 3
- 10000000
a << 3
- 01010000
~b & a
- 00001010
~a & b
- 01010000
~b << 4
- 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.
- If
arepresents a positive integer, andone = 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
-
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. Every binary digit place, from right to left, each left shift of one, raises the digit to the power of 2. -
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.
-
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
-
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
- 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
- What's the largest integer that can be represented in
nbits? ((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
-
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. There's a utf8 table to match bit patterns with, that the decoder invokes. https://www.utf8-chartable.de/unicode-utf8-table.pl?number=1024&utf8=bin, represents such a table. When the table reaches the last bit pattern of the first byte, without finding a match, there must be a control character that invokes the second byte, and so forth. I believe this is an html function. is the first line for that webpage.