Lecture 02: Computational Literacy
Tip
Video: How to use an abacus
The first mechanical calculator capable of performing all four basic arithmetic operations (addition, subtraction, multiplication, and division)
Invented by Gottfried Wilhelm Leibniz in 1694
If you took a statistics course before the late 1970s, you likely used this type of mechanical calculator for your computations
The 1970s marked the transition from mechanical to electronic:
Remember, the four digits are quarters, dimes, nickels, and pennies
What is c1112 in dollars?
What is $0.61 in coin representation? Use as few coins as you can
Practice exercise 01:
How would you write 5 in binary?
And 12?
How many bits do you need to write 11?
What is the largest number you can write with 4 bits?
To convert a binary number to decimal, just add each power of 2 that is represented by a 1
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
1001 1110 0000 1010 converts to Hex 9E0A (9 + 14 + 0 + 10)Practice Exercise 02:
Convert the decimal number 13 to binary
Convert the decimal number 13 to hexadecimal
HTML uses hexadecimal to represent colours
Six-digit hex numbers specify colours:
Each pair of digits represents a colour component (RGB)
16 * 16 = 256, so two hex digits can represent values from 0 to 255
This gives a total of 256 intensity levels for each primary colour
When you combine the three channels, you get a possible colour palette of \(256^3\) or about 16.7 million colours… but described in a compact way using hex
H, e, l, l, o, space, W, o, r, l, dA is 65, a is 97, and a space is 32“Hello World” =
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
01011001 01100001 01111001
The Apollo 11 mission to the moon was programmed in assembly language
The code is available here: https://github.com/chrislgarry/Apollo-11 (good luck reading it! 😅)
One of the files is the BURN_BABY_BURN--MASTER_IGNITION_ROUTINE.agc 🔥 🚀
But if Assembly is so fast and efficient, why don’t we use it all the time?
C++, Fortran, Go)R, Python)48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
section .data
message db 'Hello, World!', 10 ; 10 is ASCII for newline
msglen equ $ - message ; length, worked out by the assembler
section .text
global _start
_start:
mov rax, 1 ; system call number for write
mov rdi, 1 ; file descriptor 1 is stdout
mov rsi, message ; address of the string
mov rdx, msglen ; how many bytes to write
syscall ; hand over to the kernel
mov rax, 60 ; system call number for exit
xor rdi, rdi ; exit status 0
syscall ; hand over to the kernelWhat is c1112 in dollars?
$0.42 = 1 x $0.25 + 1 x $0.10 + 1 x $0.05 + 2 x $0.01
What is $0.61 in coin representation?
c2101 = 2 x $0.25 + 1 x $0.10 + 0 x $0.05 + 1 x $0.01
How would you write 5 in binary?
101, because \(4 + 1 = 5\)
How would you write 12 in binary?
1100, because \(8 + 4 = 12\)
How many bits do you need to write 11?
Four. \(11 = 8 + 2 + 1\), so 1011
What is the largest number you can write with 4 bits?
1111, which is \(8 + 4 + 2 + 1 = 15\)
In general, \(n\) bits reach \(2^n - 1\). This is why a byte stops at 255
1101 in binary.D in hexadecimal.1101.1101 (binary) = D (hex).1101 to hexadecimal:110113 corresponds to the hexadecimal number D.1101 is D in hexadecimal.Split each byte in half. Every half is one hex digit, which makes the lookup quicker.
| Binary | Halves | Hex | Decimal | Character |
|---|---|---|---|---|
01011001 |
0101 1001 |
59 | 89 | Y |
01100001 |
0110 0001 |
61 | 97 | a |
01111001 |
0111 1001 |
79 | 121 | y |
Result: Yay
Split each byte in half. Every half is one hex digit, which makes the lookup quicker.
| Binary | Halves | Hex | Decimal | Character |
|---|---|---|---|---|
01001000 |
0100 1000 |
48 | 72 | H |
01100101 |
0110 0101 |
65 | 101 | e |
01101100 |
0110 1100 |
6C | 108 | l |
01101100 |
0110 1100 |
6C | 108 | l |
01101111 |
0110 1111 |
6F | 111 | o |
00100000 |
0010 0000 |
20 | 32 | space |
01010111 |
0101 0111 |
57 | 87 | W |
01101111 |
0110 1111 |
6F | 111 | o |
01110010 |
0111 0010 |
72 | 114 | r |
01101100 |
0110 1100 |
6C | 108 | l |
01100100 |
0110 0100 |
64 | 100 | d |