generated from mwc/lab_encoding
I answered the question from 3 to 16 for lab 1: encoding.
This commit is contained in:
34
questions.md
34
questions.md
@@ -6,10 +6,6 @@ Create the following variables.
|
|||||||
a = Bits("11110000")
|
a = Bits("11110000")
|
||||||
b = Bits("10101010")
|
b = Bits("10101010")
|
||||||
```
|
```
|
||||||
print(a)
|
|
||||||
11110000
|
|
||||||
print(b)
|
|
||||||
10101010
|
|
||||||
|
|
||||||
For each of the following bytes, give an equivalent
|
For each of the following bytes, give an equivalent
|
||||||
expression which uses only `a`, `b`, and bit operators.
|
expression which uses only `a`, `b`, and bit operators.
|
||||||
@@ -25,16 +21,30 @@ The answers to the first two questions are given.
|
|||||||
|
|
||||||
3. 00000001
|
3. 00000001
|
||||||
|
|
||||||
|
a>>7
|
||||||
|
|
||||||
|
|
||||||
4. 10000000
|
4. 10000000
|
||||||
|
|
||||||
|
(a>>7)<<7
|
||||||
|
|
||||||
5. 01010000
|
5. 01010000
|
||||||
|
|
||||||
|
~b<<4
|
||||||
|
|
||||||
6. 00001010
|
6. 00001010
|
||||||
|
|
||||||
|
b>>4
|
||||||
|
|
||||||
7. 01010000
|
7. 01010000
|
||||||
|
|
||||||
|
~b<<4
|
||||||
|
|
||||||
8. 10101011
|
8. 10101011
|
||||||
|
|
||||||
|
b|(~a>>3)
|
||||||
|
|
||||||
|
|
||||||
## Integer questions
|
## Integer questions
|
||||||
|
|
||||||
These questions are difficult! Try exploring ideas with `Bits`
|
These questions are difficult! Try exploring ideas with `Bits`
|
||||||
@@ -44,9 +54,13 @@ talk with others.
|
|||||||
9. If `a` represents a positive integer, and `one = Bits(1, length=len(a))`,
|
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.
|
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
|
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.
|
to the left. (`a << 1` is twice `a`.) Explain why this trick works.
|
||||||
|
|
||||||
|
a << 1 doubles a because shifting left moves all the bits one place, so each bit’s value doubles.
|
||||||
|
|
||||||
11. Consider the following:
|
11. Consider the following:
|
||||||
```
|
```
|
||||||
>>> hundred = Bits(100, 8)
|
>>> hundred = Bits(100, 8)
|
||||||
@@ -59,17 +73,28 @@ talk with others.
|
|||||||
```
|
```
|
||||||
Apparently 100 + 100 = -56. What's going on here?
|
Apparently 100 + 100 = -56. What's going on here?
|
||||||
|
|
||||||
|
100 + 100 = 200, but we only have 8 bits, which can’t hold 200, so it becomes a negative number.
|
||||||
|
|
||||||
12. What is the bit representation of negative zero? Explain your answer.
|
12. What is the bit representation of negative zero? Explain your answer.
|
||||||
|
|
||||||
|
Negative zero is 00000000 because flipping zero and adding 1 is still zero.
|
||||||
|
|
||||||
13. What's the largest integer that can be represented in a single byte?
|
13. What's the largest integer that can be represented in a single byte?
|
||||||
Explain your reasoning.
|
Explain your reasoning.
|
||||||
|
|
||||||
|
The largest integer that can be represented in a single byte is 127 because the first bit is 0 (positive) and the other 7 bits are all 1, which makes the biggest number possible.I also checked this in the terminal using
|
||||||
|
largest = Bits("01111111"), print(largest.int).
|
||||||
|
|
||||||
14. What's the smallest integer that can be represented in a single byte?
|
14. What's the smallest integer that can be represented in a single byte?
|
||||||
Explain your reasoning.
|
Explain your reasoning.
|
||||||
|
|
||||||
|
The smallest integer that can be represented in a single byte is -128 because the first bit is 1 (negative) and the other 7 bits are all 0.
|
||||||
|
|
||||||
15. What's the largest integer that can be represented in `n` bits?
|
15. What's the largest integer that can be represented in `n` bits?
|
||||||
Explain your reasoning.
|
Explain your reasoning.
|
||||||
|
|
||||||
|
The largest integer that can be represented in n bits is when the first bit is 0 (positive) and all the other bits are 1. This makes the biggest number possible with n bits.
|
||||||
|
|
||||||
## Text questions
|
## Text questions
|
||||||
|
|
||||||
16. Look at the bits for a few different characters using the `utf8` encoding.
|
16. Look at the bits for a few different characters using the `utf8` encoding.
|
||||||
@@ -94,4 +119,5 @@ talk with others.
|
|||||||
|
|
||||||
Make a hypothesis about how this could work.
|
Make a hypothesis about how this could work.
|
||||||
|
|
||||||
|
The decoder knows how many bits to read by looking at the first bits. The pattern at the start tells it if the character is 1, 2, 3, or 4 bytes long.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user