diff options
| author | mo khan <mo@mokhan.ca> | 2024-12-11 16:52:56 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2024-12-11 16:52:56 -0700 |
| commit | 0b0cc2c592117acd9fcc433066730aa452a56c85 (patch) | |
| tree | a11efa0921f9aad0a7022307897edd33c89a5b70 | |
| parent | 42eb0d368019d7b46e198dae653e026a390eb05e (diff) | |
Complete question in assignment 2
| -rw-r--r-- | assignments/2-solution.md | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/assignments/2-solution.md b/assignments/2-solution.md index 471f970..872a910 100644 --- a/assignments/2-solution.md +++ b/assignments/2-solution.md @@ -9,7 +9,62 @@ 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: |
