cs 101 – sept. 2 chapter 3: data representation many kinds of data all in binary today: integers...

13
CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data all in binary • Today: integers – Unsigned – Signed • Later: text, images, code, etc.

Upload: marlene-berry

Post on 16-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

CS 101 – Sept. 2

Chapter 3: data representation• Many kinds of data all in binary• Today: integers

– Unsigned– Signed

• Later: text, images, code, etc.

Page 2: 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

Page 3: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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.

Page 4: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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” ?

Page 5: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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…

Page 6: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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!

Page 7: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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

Page 8: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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

Page 9: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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”

Page 10: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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

Page 11: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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.

Page 12: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

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.

Page 13: CS 101 – Sept. 2 Chapter 3: data representation Many kinds of data  all in binary Today: integers –Unsigned –Signed Later: text, images, code, etc

Recap example

• What does 110010 represent if interpreted as:– 6 bit unsigned?– 6 bit signed?