Encoding Lab- Kissane

This commit is contained in:
jkissane2
2026-03-10 21:14:40 -04:00
parent 8e08315727
commit f87f891fba
2 changed files with 30 additions and 1 deletions

View File

@@ -21,16 +21,30 @@ The answers to the first two questions are given.
3. 00000001
a>>7
4. 10000000
a<<3
5. 01010000
b<<3
6. 00001010
b>>4
7. 01010000
b<<3
8. 10101011
I cannot figure out an equivalent expression.
## Integer questions
These questions are difficult! Try exploring ideas with `Bits`
@@ -40,9 +54,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.
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 each bit left moves every bit to the next higher power of two. Every time you move to the left, your value doubles.
11. Consider the following:
```
>>> hundred = Bits(100, 8)
@@ -55,17 +73,27 @@ talk with others.
```
Apparently 100 + 100 = -56. What's going on here?
100 + 100 = 200 but since it does not fit into 8 bits, 200-256= -56.
12. What is the bit representation of negative zero? Explain your answer.
13. What's the largest integer that can be represented in a single byte?
Explain your reasoning.
The largest integer that can be represented in a signle byte is 127. As we know binary place values go up to 128. However the first digit of a positive integer must always be 0. So if we have 01111111 this number represents 64+32+16+8+4+2+1= 127.
14. What's the smallest integer that can be represented in a single byte?
Explain your reasoning.
The smallest integer that can be represented by a signle byte is -128. In order to make an integer negative it must start with 1. 10000000 results in -128.
15. What's the largest integer that can be represented in `n` bits?
Explain your reasoning.
Since the first bit is used for the sign, we are left with n-1 bits to hold the number. Bits are in base 2 so the largest integer that can be represents in n bits would be n^(n-1)-1
## Text questions
16. Look at the bits for a few different characters using the `utf8` encoding.
@@ -90,4 +118,4 @@ talk with others.
Make a hypothesis about how this could work.
I notice that each bit above gets an added 1 in the beginning and for every 1 added, it stops after another byte. For example, 'a' starts with 0 and ends after 8 bits. The second one startes with 1 and stops after 2 bytes. Then, 111 ends after 3 bytes. I predict the number of 1's in the beginning determines how many 8 bits it will be reading.