Commit fb6caf3
Changed files (2)
assignments
assignments/2-solution.md
@@ -6,15 +6,7 @@ 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)
-
+```plaintext
| 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 |
@@ -22,49 +14,67 @@ Chapter 4:
| 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
+ 8.times.map { |x| 2**x }.reverse
+ => [128, 64, 32, 16, 8, 4, 2, 1]
+ 8.times.map { |x| 4**x }.reverse
+ => [16384, 4096, 1024, 256, 64, 16, 4, 1]
+ 8.times.map { |x| 8**x }.reverse
+ => [2097152, 262144, 32768, 4096, 512, 64, 8, 1]
+ 8.times.map { |x| 10**x }.reverse
+ => [10000000, 1000000, 100000, 10000, 1000, 100, 10, 1]
+ 8.times.map { |x| 16**x }.reverse
+ => [268435456, 16777216, 1048576, 65536, 4096, 256, 16, 1]
+ ```
+
+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)
+
+ ```plaintext
+ | 64 | 16 | 4 | 1 |
+ | -- | -- | - | - |
+ | 0 | 1 | 3 | 3 |
+ ```
+
+ ```ruby
+ (1*16) + (3*4) + (3*1)
+ => 31
+ 31.to_s(4)
+ => "133"
+ ```
+
+ b. 367 (base 8, also called octal) = 247 (base 10)
- ```ruby
- (1*16) + (3*4) + (3*1)
- => 31
- ```
+ ```plaintext
+ | 512 | 64 | 8 | 1 |
+ | --- | -- | - | - |
+ | 0 | 3 | 6 | 7 |
+ ```
- b.
- | 512 | 64 | 8 | 1 |
- | --- | -- | - | - |
- | 0 | 3 | 6 | 7 |
+ ```ruby
+ (3*64) + (6*8) + (7*1)
+ => 247
+ 247.to_s(8)
+ => "367"
+ ```
- ```ruby
- (3*64) + (6*8) + (7*1)
- => 247
- ```
+ c. 1BA (base 16, also called hexadecimal. B is the digit that represents 11; A is the digit that represents 10.) = 442 (base 10)
- c.
- | 4,096 | 256 | 16 | 1 |
- | ----- | --- | -- | -- |
- | 0 | 1 | B | A |
- | 0 | 1 | 11 | 10 |
+ ```plaintext
+ | 4,096 | 256 | 16 | 1 |
+ | ----- | --- | -- | -- |
+ | 0 | 1 | B | A |
+ | 0 | 1 | 11 | 10 |
+ ```
- ```ruby
- (1*256) + (11*16) + (10*1)
- => 442
- ```
+ ```ruby
+ (1*256) + (11*16) + (10*1)
+ => 442
+ 442.to_s(16)
+ => "1ba"
+ ```
Chapter 5:
3431709-assignment-2.pdf
Binary file