CS 101 – Sept. 2
Chapter 3: data representation• Many kinds of data all in binary• Today: integers
– Unsigned– Signed
• Later: text, images, code, etc.
Different kinds of data!
Kind of data File type Software used
Numbers.xlsx
.accdb
Excel
Access
Text
.txt
.docx
.html
NotePad
MS Word
Dreamweaver
Image .gif Photoshop
Instructions .exe The operating system
Converting to binary
• A computer system has both HW & SW• HW: only understands binary• SW: Translates human information into binary for
the HW.• How?
– Break into pieces (digits, words, letters, symbols, pixels, …)
– Each piece individually gets a binary number.
Data vs. information
• What does 29613 mean to you? Or is it just a random number?
• What might 111010110101110010… mean to the computer?
• Information means that we are adding “meaning” to the data
• Binary is like the alphabet… all languages use it to give meaning to letters. What is “pet set” ?
Analog vs. Digital
• Analog is how WE understand the world
• Just “warmer” or “crooked” is enough for us
• Can handle “in between” values
• Think of a clock, speedometer, odometer
• Digital emphasizes exact values, usually expressed as 0 or 1, true or false.
• No such thing as “in between.” It’s either 10:30 or 10:31…
An analogy
Think of the notes on a piano.• 88 keys• Range: from lowest to highest key.• Granularity: distance between consecutive notes.• But for computer, notes are converted to binary
numbers!
Representations
• To represent anything in binary, need to know:– Number of bits? (size)
– What do the bits mean? (scheme)
• Representation schemes for integers:– unsigned– signed– sign magnitude– BCD
Size of rep’n
• Most often 8, 16, or 32 bits
• For our examples, we’ll use small sizes
• 5 bits: 25 different numbers
• 32 bits: 232 different numbers
Scheme I: Unsigned
• Ordinary binary number. • For 5 bits: smallest = 00000 (zero)
largest = 11111 (31)
• In general, smallest = 0, largest = 2n – 1.
• Going beyond possible range: “overflow”
Scheme II: Signed
• Allows for negative numbers
• We want half the rep’ns to be negative.
• Ex. 5 bits 32 numbers.– 16 numbers are negative– Thus, range is –16 to +15.
• For n bits: range is –2n–1 to 2n – 1 – 1
How to represent negatives
• In unsigned, impossible!
• In signed: 3 steps to represent –x:– Find rep’n of +x.– Invert the bits.– Add 1.
• Try some examples.
Closer look at signed
• In 5 bits: largest number is 01111 (+15)
lowest number is 10000 (–16)
• Leftmost bit is sign bit
• Positive #’s have same rep’n as unsigned.
• Technique for –x doesn’t work for –16.
Recap example
• What does 110010 represent if interpreted as:– 6 bit unsigned?– 6 bit signed?