generated from mwc/lab_encoding
I answered the questions for both booleans and integers
This commit is contained in:
22
questions.md
22
questions.md
@@ -12,24 +12,28 @@ expression which uses only `a`, `b`, and bit operators.
|
|||||||
The answers to the first two questions are given.
|
The answers to the first two questions are given.
|
||||||
|
|
||||||
1. 01010101
|
1. 01010101
|
||||||
|
|
||||||
~b
|
~b
|
||||||
|
|
||||||
2. 00000101
|
2. 00000101
|
||||||
|
|
||||||
~a & ~b
|
~a & ~b
|
||||||
|
|
||||||
3. 00000001
|
3. 00000001
|
||||||
|
a >> 7
|
||||||
|
|
||||||
4. 10000000
|
4. 10000000
|
||||||
|
a << 3
|
||||||
|
|
||||||
5. 01010000
|
5. 01010000
|
||||||
|
a & ~b
|
||||||
|
|
||||||
6. 00001010
|
6. 00001010
|
||||||
|
~a & b
|
||||||
|
|
||||||
7. 01010000
|
7. 01010000
|
||||||
|
a & ~b
|
||||||
|
|
||||||
8. 10101011
|
8. 10101011
|
||||||
|
b | (a >> 7)
|
||||||
|
|
||||||
## Integer questions
|
## Integer questions
|
||||||
|
|
||||||
@@ -40,8 +44,11 @@ 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.
|
||||||
|
This trick works because every spot to the left is double of the right so moving everything will double the entire number
|
||||||
|
|
||||||
11. Consider the following:
|
11. Consider the following:
|
||||||
```
|
```
|
||||||
@@ -55,16 +62,27 @@ talk with others.
|
|||||||
```
|
```
|
||||||
Apparently 100 + 100 = -56. What's going on here?
|
Apparently 100 + 100 = -56. What's going on here?
|
||||||
|
|
||||||
|
the full 8-bit is equal to 256 so 200 mod 256 is the same as -56 mod 256
|
||||||
|
|
||||||
12. What is the bit representation of negative zero? Explain your answer.
|
12. What is the bit representation of negative zero? Explain your answer.
|
||||||
|
zero is neither negative or positive, it is just the absence of a value so it would just be 00000000
|
||||||
|
|
||||||
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 by a single byte is 127.
|
||||||
|
A byte is 8 bits, since the very left bit is positive with a 0, the highest representation would be 01111111 which is equal to 127
|
||||||
|
|
||||||
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 by a single byte is -128.
|
||||||
|
Similar to problem 13, to have the smallest integer we need the largest negative number which is only represented by the very left number.
|
||||||
|
Therefore it would be 10000000 because we are not adding any positives to it.
|
||||||
|
|
||||||
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 2^(n-1) -1. Based on the example in 13 this pattern makes sense,
|
||||||
|
we have to do n-1 because the first number is negative and can not represent a positive integer. We also have to subtract 1
|
||||||
|
because it is one less.
|
||||||
|
|
||||||
## Text questions
|
## Text questions
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user