generated from mwc/lab_encoding
Finished the lab
This commit is contained in:
16
questions.md
16
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) + one
|
||||
|
||||
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.
|
||||
|
||||
shifting left is the same as multiplying by 2 becouse each shift moves every bit to a position worth twice as much
|
||||
|
||||
11. Consider the following:
|
||||
```
|
||||
>>> hundred = Bits(100, 8)
|
||||
@@ -67,17 +71,27 @@ talk with others.
|
||||
```
|
||||
Apparently 100 + 100 = -56. What's going on here?
|
||||
|
||||
100 + 100 results in -56 because the sum (200) exceeds the maximum value that can be stored in 8 bits, causing an overflow and making the binary result interpreted as a negative number using twos complement.
|
||||
|
||||
12. What is the bit representation of negative zero? Explain your answer.
|
||||
|
||||
negative zero has the same bit representation as zero (00000000) because flipping all bits of zero and adding one results in zero again, so there is no distinct negative zero.
|
||||
|
||||
13. What's the largest integer that can be represented in a single byte?
|
||||
Explain your reasoning.
|
||||
|
||||
The largest integer in a single byte is 127 because, the leftmost bit is reserved for the sign, leaving 7 bits for the value, which gives a maximum of 01111111 = 127.
|
||||
|
||||
14. What's the smallest integer that can be represented in a single byte?
|
||||
Explain your reasoning.
|
||||
|
||||
The smallest integer in a single byte is -128 because in 8-bit, the bit pattern 10000000 represents -128, which is the lowest possible value when the leftmost bit is used as the sign.
|
||||
|
||||
15. What's the largest integer that can be represented in `n` bits?
|
||||
Explain your reasoning.
|
||||
|
||||
|
||||
|
||||
## Text questions
|
||||
|
||||
16. Look at the bits for a few different characters using the `utf8` encoding.
|
||||
@@ -102,4 +116,6 @@ talk with others.
|
||||
|
||||
Make a hypothesis about how this could work.
|
||||
|
||||
The decoder looks at the first 8 bits of a byte to determine how many total bytes the character uses.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user