summaryrefslogtreecommitdiff
path: root/assignments/2-solution.md
blob: 872a910b3cd9e374e0aa61f04cf35b8bedfc61a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# COMP-200: Intro to Computer Systems
## Assignment 2
### mo khan - 3431709

Choose ONE exercise each from Chapters 4 and 5

Chapter 4:

1. Given our discussion of positional numbering systems in Section 4.2.1, see whether you can determine the decimal value of the following numbers:

  a. 133 (base 4)
    - 31 (base 10)
  b. 367 (base 8, also called octal)
    - 247 (base 10)
  c. 1BA (base 16, also called hexadecimal. B is the digit that represents 11; A is the digit that represents 10.)
    - 442 (base 10)

| Base (N) | N^8         | N^7        | N^6       | N^5    | N^4   | N^3 | N^2 | N^1 |
| ----     | -           | -          | -         | -      | -     | -   | -   | -   |
| 2        | 128         | 64         | 32        | 16     | 8     | 4   | 2   | 1   |
| 4        | 16,384      | 4,096      | 1,024     | 256    | 64    | 16  | 4   | 1   |
| 8        | 2097152     | 262144     | 32768     | 4096   | 512   | 64  | 8   | 1   |
| 10       | 10,000,000  | 1,000,000  | 100,000   | 10,000 | 1,000 | 100 | 10  | 1   |
| 16       | 268,435,456 | 16,777,216 | 1,048,576 | 65,536 | 4,096 | 256 | 16  | 1   |

```irb
irb(main):025> 8.times.map { |x| 2**x }.reverse
=> [128, 64, 32, 16, 8, 4, 2, 1]
irb(main):031> 8.times.map { |x| 4**x }.reverse
=> [16384, 4096, 1024, 256, 64, 16, 4, 1]
irb(main):032> 8.times.map { |x| 8**x }.reverse
=> [2097152, 262144, 32768, 4096, 512, 64, 8, 1]
irb(main):026> 8.times.map { |x| 10**x }.reverse
=> [10000000, 1000000, 100000, 10000, 1000, 100, 10, 1]
irb(main):037> 8.times.map { |x| 16**x }.reverse
=> [268435456, 16777216, 1048576, 65536, 4096, 256, 16, 1]
```

  a.
    | 64 | 16 | 4 | 1 |
    | -- | -- | - | - |
    | 0  | 1  | 3 | 3 |

    ```ruby
    (1*16) + (3*4) + (3*1)
    => 31
    ```

  b.
    | 512 | 64 | 8 | 1 |
    | --- | -- | - | - |
    |   0 | 3  | 6 | 7 |

    ```ruby
    (3*64) + (6*8) + (7*1)
    => 247
    ```

  c.
    | 4,096 | 256 | 16 |  1 |
    | ----- | --- | -- | -- |
    |     0 |   1 |  B |  A |
    |     0 |   1 | 11 | 10 |

    ```ruby
    (1*256) + (11*16) + (10*1)
    => 442
    ```

Chapter 5: