generated from mwc/lab_encoding
questions.md
This commit is contained in:
49
questions.md
49
questions.md
@@ -21,16 +21,28 @@ The answers to the first two questions are given.
|
|||||||
|
|
||||||
3. 00000001
|
3. 00000001
|
||||||
|
|
||||||
|
~a >> 3
|
||||||
|
|
||||||
4. 10000000
|
4. 10000000
|
||||||
|
|
||||||
|
a << 3
|
||||||
|
|
||||||
5. 01010000
|
5. 01010000
|
||||||
|
|
||||||
|
~b & a
|
||||||
|
|
||||||
6. 00001010
|
6. 00001010
|
||||||
|
|
||||||
|
~a & b
|
||||||
|
|
||||||
7. 01010000
|
7. 01010000
|
||||||
|
|
||||||
|
~b << 4
|
||||||
|
|
||||||
8. 10101011
|
8. 10101011
|
||||||
|
|
||||||
|
~a >> 3 | b
|
||||||
|
|
||||||
## Integer questions
|
## Integer questions
|
||||||
|
|
||||||
These questions are difficult! Try exploring ideas with `Bits`
|
These questions are difficult! Try exploring ideas with `Bits`
|
||||||
@@ -40,8 +52,25 @@ 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=91
|
||||||
|
>>>a
|
||||||
|
01011011
|
||||||
|
>>>one=Bits(1, 8)
|
||||||
|
>>>one
|
||||||
|
00000001
|
||||||
|
>>>~a
|
||||||
|
10100100
|
||||||
|
**>>>~a + one**
|
||||||
|
10100101
|
||||||
|
>>>-a
|
||||||
|
10100101
|
||||||
|
>>>-a.int
|
||||||
|
-91
|
||||||
|
|
||||||
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.
|
||||||
|
Every binary digit place, from right to left, each left shift of one,
|
||||||
|
raises the digit to the power of 2.
|
||||||
|
|
||||||
11. Consider the following:
|
11. Consider the following:
|
||||||
```
|
```
|
||||||
@@ -54,17 +83,27 @@ talk with others.
|
|||||||
-56
|
-56
|
||||||
```
|
```
|
||||||
Apparently 100 + 100 = -56. What's going on here?
|
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.
|
||||||
|
|
||||||
12. What is the bit representation of negative zero? Explain your answer.
|
12. 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
|
||||||
|
|
||||||
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? (8 bits in a byte).
|
||||||
Explain your reasoning.
|
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
|
||||||
|
|
||||||
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? (8 bits in a byte).
|
||||||
Explain your reasoning.
|
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
|
||||||
|
|
||||||
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? ((2**n)/2) - 1.
|
||||||
Explain your reasoning.
|
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
|
## Text questions
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user