CS 208 w20 lecture 0 outline
1 Preview/Motivation
- memory is finite
- overflow example
- will 200*300*400*500 be positive or negative?
int_test.java
- will 200*300*400*500 be positive or negative?
- floating point
- is multiplication associative (e.g., are
(2.5*0.1)*1.5
and2.5*(0.1*1.5)
equivalent?)
- is multiplication associative (e.g., are
- overflow example
- constant factors matter
- computing the factorial iteratively vs recursively, which will be faster?
factorial_test.java
- looping over rows then columns or columns then rows, does it matter?
loops_test.java
- computing the factorial iteratively vs recursively, which will be faster?
- layers of abstraction
- say I'm streaming music from Spotify and writing code at the same time on my laptop—what tasks does it have to perform?
- receive and process network packets
- send data to audio device
- update screen
- write to disk
- run compiler/executable
- each program operates under the convenient illusion that it is the only one using these resources
- OS makes it so that application developers don't have to worry about the mess underneath
- nor can one application go and actively mess around with memory data for another (virtual memory)
- OS and hardware handle the rapid switching between all of these tasks to give a smooth real-time appearance
- say I'm streaming music from Spotify and writing code at the same time on my laptop—what tasks does it have to perform?
- 208 focuses on the hardware/software interface from a programmer's perspective
- mainly the Instruction Set Architecture, Operating System, and Compiler/Interpreter in this diagram of computer system layers
2 Administrivia
- course web page has it all: http://cs.carleton.edu/faculty/awb/cs208/w20/
- calendar, course info, useful links, grading policy, late day policy, academic integrity policy
3 Binary & hexadecimal
- binary = base 2
- base 10 (regular decimal numbers) has a 1s, 10s, 100s digit, etc.
- the number 23 is \(2\times 10 + 3 \times 1\)
- can be written as \(10^0\), \(10^1\), \(10^2\), …
- hence base 10 numbers
- binary just substitutes a base of 2 for a base of 10
- \(2^0\), \(2^1\), \(2^2\), …
- 1s, 2s, 4s, …
- the binary number 11 is \(1\times 2 + 1\times 1\) or 3
- the base of a numbering system also indicates how many unique digits it has
- base 10 has the digits 0–9, after 9 we carry over to the next larger place
- base 2 has two digits, 0 and 1
- why use base 2 for computing? natural fit with presence/absence of electric current or high vs low voltage
- a single binary digit is called a bit, the fundamental unit of digital data
- it's also very common to talk about data in terms of bytes (8 bits to a byte)
- QUICK CHECK: how many bits do you need to represent the base 10 number 17?
- 5 bits (
0b10001
), binary numbers indicated with 0b prefix
- 5 bits (
- QUICK CHECK: how many different values can 6 bits represent?
- 64 (from 0,
0b000000
, to 63,0b111111
)
- 64 (from 0,
- base 10 (regular decimal numbers) has a 1s, 10s, 100s digit, etc.
- hexadecimal = base 16
- same principle as before
- \(16^0\), \(16^1\), \(16^2\), … \(\rightarrow\) 1s, 16s, 256s
- but why base 16?
- first, base 10 doesn't fit naturally with binary since 10 isn't a power of 2
- 16 is \(2^4\), (how many bits?), giving it the nice property of 4 bits to represent each digit, 2 hex digits to a byte
- a single hex digit, or 4 bits, is called a nybble
- speaking of hex digits, the base 10 0 to 9 aren't enough
0x10
(hex numbers indicated with 0x prefix) is 16 in base 10, we need digits for 10–15- use the letters a–f
- same principle as before
base 10 | base 2 | base 16 |
---|---|---|
0 | 0b0000 |
0x0 |
1 | 0b0001 |
0x1 |
2 | 0b0010 |
0x2 |
3 | 0b0011 |
0x3 |
4 | 0b0100 |
0x4 |
5 | 0b0101 |
0x5 |
6 | 0b0110 |
0x6 |
7 | 0b0111 |
0x7 |
8 | 0b1000 |
0x8 |
9 | 0b1001 |
0x9 |
10 | 0b1010 |
0xa |
11 | 0b1011 |
0xb |
12 | 0b1100 |
0xc |
13 | 0b1101 |
0xd |
14 | 0b1110 |
0xe |
15 | 0b1111 |
0xf |
16 | 0b10000 |
0x10 |
- QUICK CHECK: what is
0xaf
in binary and decimal?
3.1 Everything is bits!
- consider
0x4e6f21
, what does it mean?- base 10 integer 5140257
- the characters "No!" (http://www.asciitable.com)
- a nice olive green color (check it out)
- the real number \(7.20303424\times 10^{-39}\)
- up to the program/programmer to interpret what any sequence of bits actually represents