270 likes | 361 Views
CS120: Lecture 2. MP Johnson Hunter College mpjohnson@gmail.com. Agenda: data. Review ops/gates Abstract storage: flipflops Storage methods representation: Test Images Sound Numbers Positive Negative Fractions Compression. Circuits as memory.
E N D
CS120: Lecture 2 MP Johnson Hunter College mpjohnson@gmail.com
Agenda: data • Review ops/gates • Abstract storage: flipflops • Storage methods • representation: • Test • Images • Sound • Numbers • Positive • Negative • Fractions • Compression
Circuits as memory • Flipflop: special circuit that can store data • 1 bit per FF To store, send pulse in top or bottom (0 is ddf) Go through
Another FF Go through
circuits • Q: where do inputs come from? • A: other circuits! • Connect worker circuit to mem circuit • This kind of mem is in RAM or maybe CPU • Fast: measured in ns • Fast everywhere – “random” • But Requires constant power
Metric units • “Kilo-” normally means 1,000;Kilobyte = 210 = 1024 • “Mega-” normally means 1,000,000;Megabyte = 220 = 1,048,576 • “Giga-” normally means 1,000,000,000;Megabyte = 230 = 1,073,741,824
Other (perm) kinds of mem • Hard drive: Disk spins, head moves • Partly mechanical! • Usually larger, but slower • Measured in ms • Time depends somewhat on location
Other (perm) kinds of mem • Optical: CD/DVD • Slower, less random • Read-only/read-once • Flash: USB, MP3 • Tape: very slow, sequential
Storage devices • Hardware varies a lot • Q: what stays the same? • A: all store bits / 1-of-2 choices • Q: why not base 3 or base 10? • A: easier! • High (voltage) v. low or pos v neg or 0 v 1 • High v. low v. medium • Q: why do we ever use base 10?
Claim: data = nums = bits • Eg, letters: can’t write ‘A’ on a HD • Q: what to do? • A: pick some num to rep ‘A’ • Turns out: ‘A’ = 65, ‘B’ = 66 • ANSI’s ASCII • Q: but how to rep 65? • Can’t write num 65 on a HD • A: write 65 in binary
base • mathematically, any base will do • On machine, must use base 2: • 65 = 6*10 + 5*1 = 1*64 + 1*1 = 1*2^5 + 1*2^0 = 1000001b • 65 + 1 = 1000001 + 1 = 1000010
ANSI • ANSI only uses 7 bits (128 vals) • Insert a 1 at front to get 8 bits = 1 byte (255) • Usually: think in terms of bytes, not bits • Q: what about negatives, fractions? • A: next time…
hex • Sometimes base-16 is used for as shorthand • 1 base-16 val = 4 bits • Also octal
Rep’ing images? • One way: • Bitmaps (.bmp) • b/w: 2-d table of brightness values • Pixel = picture element • Col: one table each of r/g/b • Size: 1000*1000*3 = 3MB! • Another: • Vector methods…
Rep’ing sounds? • One way: • Sample soundwave ampl at intervals • Phones: 8000/s • CD: 44,1000/s • Another: • MIDI • For 10s constant tone, record tone + length
Data compression • NB: vectors and MIDI sound smarter • But whatever you do, must still be stored as bits • Must find a way to encode these instructions as bits • Harder than storing directly!
Convert base10 base2 • Alg: while x is not 0 • Write x%2 to right • Set x = x/2
Integers with bits • Unsigned (non-neg): just base2 • Signed: 0. Just use sign bit • How to add/sub? • 1’s comp • How to add/sub? • Two 0’s • 2’s comp • How to add/sub? • See app. B for neg, add circuits • Also: excess notation (skip?)
IEEE floats • Used for all fractions in C/C++/Java • Strength: very large range (billions) • Weakness: merely approximate • Can depend on order!
Compression • Saw MIDI for sound • Saw vectors for images • Other methods: • Relative methods for images (why works?) • GIF, JPEG • Relative methods for video (why works?) • MPEG • Run-length encoding • Frequency-based (why works?) • LZ (zip files)
Recognizing errors • Idea: include extra info • Parity bits:
Correcting errors • “Error-correcting codes” • If only one bad bit, can recover,e.g., 010100
Future • No class Tuesday • Reading for Wednesday is on website • Hw posted soon (email/web)