slides

9/4/15
1930s
1940s
1950s
1960s
1970s
1980s
1990s
2000s
2010s
Modern Digital Computer
“von Neumann” model
HW-­‐
controlled
representing data with bits
?
bits, bytes, numbers, and notation
Processor
instructions
data
SW-­‐
controlled
Memory
How are data and instructions represented?
positional number representation
bit = binary digit = 0 or 1
fancy name, familiar concept
2 4 0
Electronically: high voltage vs. low voltage
0
1
0
100
10 2
2
3.3V
2.8V
0.5V
0.0V
10
10 1
1
1
10 0
0
= 2 x 102 + 4 x 101 + 0 x 100
weight
position
• Base determines:
– Maximum digit (base – 1). M inimum digit is 0 .
– Weight of each position.
Basis of all digital representations
ints, floats, c hars, strings, booleans, etc.
machine i nstructions
• Each position holds a digit.
• Represented value = sum of all position values
– Position value = digit value x b aseposition
3
index
4
1
9/4/15
base 2 (binary)
1 0 1 1
8
23
3
4
22
2
2
21
1
1
20
0
conversion and arithmetic
1910 = ? 2
= 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0
One strategy:
Subtract largest power of 2 t hat is <= 19 from 1910 and at it to 02.
Repeat w ith remaining10 and sum2 until 010 remains.
weight
position
24010 = ? 2
110100112 = ? 10
1012 + 10112 = ? 2
10010112 x 210 = ? 2
When ambiguous, subscript with base:
101 10 Dalmatians
101 2-­‐Second Rule
(movie)
(folk wisdom about d ropped food)
5
6
What do you call 4 bits?
numbers and wires
• One wire per bit
• How many wires do w e need…?
byte = 8 bits
a.k.a. octet
Smallest unit of data
used b y a typical modern computer
Binary 000000002 -­‐-­‐ 111111112
Decimal 00010 -­‐-­‐ 25510
Hexadecimal 0016 -­‐-­‐ FF 16
Byte = 2 hex digits!
• Fixed-­‐size encodings to build hardware.
Programmer’s hex notation ( C, etc.):
0xB4 = B4 16
Octal (base 8) also useful.
Why do 2 40 students o ften confuse Halloween and C hristmas?
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
8
2
9/4/15
word |wərd|, n.
C programming language
Natural unit of data used by processor.
• Invented i n 1970s to help build UNIX operating system
– Fixed size (e.g. 32 bits, 6 4 bits)
– OS manages hardware, C close to machine model
• depends on ISA: Instruction Set Architecture
• Simple pieces l ook like J ava:
– machine i nstructions usually operate on words
– word size = register size = address size
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
0
0
0
0
0
0 0
0
1
0
1
0
1
1
1
1
1
0
0
0
0
0
0 0 0 0
– if, while, for, local variables, assignment, etc.
5
4
3
2
1
0
1
0
0
0
0
0
• Other pieces do not:
– no objects, no methods, no array bounds checks
– addresses, pointers, structs, functions, weak type system
Java/C int = 4 bytes: 1 1,501,584 • Important l anguage, still widely used,
but many good PL i deas have c ome along since.
9
fixed-­‐size data representations
boolean algebra, C notation
(size in bytes)
Java Data Type C Data Type
boolean
byte
char
short
int
float
double
long
char
short int
int
float
long int
double
long long
long d ouble
AND: A&B
& 0 1
0 0 0
1 0 1
OR: A|B
| 0 1
0 0 1
1 1 1
XOR: A^B
^ 0 1
0 0 1
1 1 0
NOT: ~A
~
0 1
1 0
32-­‐bit 64-­‐bit
1
1
2
2
4
4
4
8
8
8
1
1
2
2
4
4
8
8
8
16
Depends on word size!
11
12
3
9/4/15
general boolean algebras
sets as bit vectors
Representation
w-­‐bit vector represents subsets of {0, … , w–1}.
ai = 1 ≡ i ∈ A
Operate on bit vectors with bitwise operators.
01101001
& 01010101
01000001
01101001
| 01010101
01101001
^ 01010101
Laws of boolean algebra apply.
~ 01010101
01010101
^ 01010101
01101001
76543210
{ 0 , 3 , 5 , 6 }
01010101
76543210
{ 0 , 2 , 4 , 6 }
Operations
& | ^
~
e.g., DeMorgan’s Law: ~(A | B) = ~A & ~B
How does this relate to set operations?
Intersection
Union
Symmetric difference
Complement
01000001
01111101
00111100
10101010
{ 0 , 6 }
{ 0 , 2 , 3 , 4 , 5, 6 }
{ 2 , 3 , 4 , 5 }
{ 1 , 3 , 5 , 7 }
13
bitwise operations in C
& | ^ ~
14
logical operations in C
&& || !
apply to any “ integral” data type
long, int, short, char, unsigned
apply to any “ integral” data type
long, int, short, char, unsigned
0 is false
nonzero is true
result always 0 or 1
Early termination a.k.a. s hort-­‐circuit evaluation
Examples (char)
~0x41 =
~0x00 =
Examples (char)
!0x41 =
!0x00 =
!!0x41 =
0x69 & 0x55 =
0x69 | 0x55 =
0x69 && 0x55 =
0x69 || 0x55 =
Many bit-­‐twiddling puzzles i n first assignment
15
16
4