54
CMSC 150 DATA REPRESENTATION CS 150: Mon 30 Jan 2012

CMSC 150 data representation

  • Upload
    meir

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

CMSC 150 data representation. CS 150: Mon 30 Jan 2012. What Happens When…. Steps Of An Executing Program. Initially, the program resides as a (binary) file on the disk. Steps Of An Executing Program. When you click an icon to start a program…. Steps Of An Executing Program. - PowerPoint PPT Presentation

Citation preview

Page 1: CMSC 150 data representation

CMSC 150DATA

REPRESENTATION

CS 150: Mon 30 Jan 2012

Page 2: CMSC 150 data representation

What Happens When…2

Page 3: CMSC 150 data representation

Steps Of An Executing Program

3

Initially, the program resides as a (binary) file

on the disk

Page 4: CMSC 150 data representation

4

Steps Of An Executing Program

When you click an icon to start a

program…

Page 5: CMSC 150 data representation

5

Steps Of An Executing Program

the program is copied from disk

into memory (RAM)…

Page 6: CMSC 150 data representation

6

Steps Of An Executing Program

so the program can be executed by the CPU…

Page 7: CMSC 150 data representation

127128129130131132

01010010

133134

.

.

.

……

01010110 01110010 11010010 10110010 10111110 11100000 00000001

01010010 11100000

A Program in Memory Program: consists of

instructions & data

Instructions & data stored together in memory

Page 8: CMSC 150 data representation

127128129130131132

01010010

133134

.

.

.

……

01010110 01110010 11010010 10110010 10111110 11100000 00000001

01010010 11100000

Instructions

A Program in Memory

Page 9: CMSC 150 data representation

127128129130131132

01010010

133134

.

.

.

……

01010110 01110010 11010010 10110010 10111110 11100000 00000001

01010010 11100000

A Program in Memory

Instructions

Data

Page 10: CMSC 150 data representation

127128129130131132

01010010

133134

.

.

.

……

01010110 01110010 11010010 10110010 10111110 11100000 00000001

01010010 11100000

A Program in Memory

Instructions

Data

How do we interpret these 0's and 1's?

Page 11: CMSC 150 data representation

The Decimal Number System

Deci-

Base is ten first (rightmost) place: ones (i.e., 100) second place: tens (i.e., 101) third place: hundreds (i.e., 102) …

Digits available: 0, 1, 2, …, 9 (ten total)

Page 12: CMSC 150 data representation

The Binary Number System Bi- (two)

bicycle, bicentennial, biphenyl

Base two first (rightmost) place: ones (i.e., 20) second place: twos (i.e., 21) third place: fours (i.e., 22) …

Digits available: 0, 1 (two total)

Page 13: CMSC 150 data representation

Lingo Bit (b): one binary digit (0 or 1)

Byte (B): eight bits

Prefixes: Kilo (K) = 210 = 1024 Mega (M) = 220 = 1,048,576 = 210 K Giga (G) = 230 = 1,073,741,824 = 210 M Tera (T) = 240 = 1,099,511,627,776 = 210 G Peta (P) = 250 = 1,125,899,906,842,624 = 210 T … Yotta (Y) = 280 = 1,208,925,819,614,629,174,706,176 = 230 P

Page 14: CMSC 150 data representation

Representing Decimal in Binary Moving right to left, include a "slot" for

every power of two ≤ your decimal number

Then, moving left to right: Put 1 in the slot if that power of two can be

subtracted from your total remaining Put 0 in the slot if not Continue until all slots are filled

filling to the right with 0's as necessary

Page 15: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

100001000101111111101101

Page 16: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910

=

?????????????????????2

What is 210 ?

Page 17: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910

=

?????????????????????2

What is 210 ? 220 ?

Page 18: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910

=

?????????????????????2

What is 210 ? 220 ? 230 ?

Page 19: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910

=

?????????????????????2

What is 210 ? 220 ? 230 ? 225 ?

Page 20: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910 =

?????????????????????2

What is 210 ? 220 ? 230 ? 225 ? Need 24 places (bits)

223 = 8,388,608

Page 21: CMSC 150 data representation

To the whiteboard,

Robin !

Page 22: CMSC 150 data representation

Example: A Famous Number Anyone recognize this number ?

8,675,30910

=

1000010001011111111011012

Fewer available digits in binary more space required for representation

Page 23: CMSC 150 data representation

Converting Binary to Decimal

For each 1, add the corresponding power of two

10100101111012

Page 24: CMSC 150 data representation

Converting Binary to Decimal

For each 1, add the corresponding power of two

10100101111012 = 530910

Page 25: CMSC 150 data representation

Now You Get The Joke

THERE ARE 10 TYPES OF PEOPLE IN THE WORLD:

THOSE WHO CAN COUNT IN BINARY

AND THOSE WHO CAN'T

Page 26: CMSC 150 data representation

Why Binary?

A B A xor B A and B

0 0

0 1

1 0

1 1

Circuits: low voltage == 0, high voltage == 1

Page 27: CMSC 150 data representation

Why Binary?

A B A xor B A and B

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

Circuits: low voltage == 0, high voltage == 1

Page 28: CMSC 150 data representation

Why Binary?

A B A xor B A and B

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

A + B sumA + B carry

Circuits: low voltage == 0, high voltage == 1

Half Adder

Page 29: CMSC 150 data representation

An Alternative to Binary? 1000010001011111111011012 = 8,675,30910

1000001001011111111011012 = 8,544,23710

Page 30: CMSC 150 data representation

An Alternative to Binary? 1000010001011111111011012 = 8,675,30910

1000001001011111111011012 = 8,544,23710

What if this was km to landing?

Page 31: CMSC 150 data representation

The Hexadecimal Number System

Hexa- (six) Decimal (ten) Base sixteen

first (rightmost) place: ones (i.e., 160) second place: sixteens (i.e., 161) third place: two-hundred-fifty-sixes (i.e., 162) …

Digits available: sixteen total 0, 1, 2, …, 9, A, B, C, D, E, F

Page 32: CMSC 150 data representation

Wikipedia says… Donald Knuth has pointed out

that the etymologically correct term is "senidenary", from the Latin term for "grouped by 16".

The terms "binary", "ternary" and "quaternary" are from the same Latin construction, and the etymologically correct term for "decimal" arithmetic is "denary".

Schwartzman notes that the pure expectation from the form of usual Latin-type phrasing would be "sexadecimal", but then computer hackers would be tempted to shorten the word to "sex".

Page 33: CMSC 150 data representation

Using Hex Can convert decimal to hex and vice-

versa process is similar, but using base 16 and 0-

9, A-F

Most commonly used as a shorthand for binary

Avoid this

Page 34: CMSC 150 data representation

More About Binary How many different things can you

represent using binary:

with only one slot (i.e., one bit)? with two slots (i.e., two bits)? with three bits? with n bits?

Page 35: CMSC 150 data representation

More About Binary How many different things can you

represent using binary:

with only one slot (i.e., one bit)? 2 with two slots (i.e., two bits)? 22 = 4 with three bits? 23 = 8 with n bits? 2n

Page 36: CMSC 150 data representation

Binary vs. Hex One slot in hex can be one of 16 values

0, 1, 2, …, 9, A, B, C, D, E, F

How many bits to represent one hex digit?

I.e., how many bits to represent 16 different things?

Page 37: CMSC 150 data representation

Binary vs. Hex One slot in hex can be one of 16 values

0, 1, 2, …, 9, A, B, C, D, E, F

How many bits to represent one hex digit?

4 bits can represent 24 = 16 different values

Page 38: CMSC 150 data representation

Binary vs. Hex0 00001 00012 00103 00114 01005 01016 01107 0111

8 10009 1001A 1010B 1011C 1100D 1101E 1110F 1111

Page 39: CMSC 150 data representation

Converting Binary to Hex Moving right to left, group into bits of

four Convert each four-group to

corresponding hex digit

1000010001011111111011012

Page 40: CMSC 150 data representation

Converting Binary to Hex Moving right to left, group into bits of

four Convert each four-group to

corresponding hex digit

1000 0100 0101 1111 1110 11012

Page 41: CMSC 150 data representation

Converting Binary to Hex Moving right to left, group into bits of

four Convert each four-group to

corresponding hex digit

1000 0100 0101 1111 1110 11012 8 4 5 F E D

1000010001011111111011012 = 845FED16

Page 42: CMSC 150 data representation

Converting Hex to Binary Convert each hex digit to four-bit binary

equivalent

BEEF16 = ????2

Page 43: CMSC 150 data representation

Converting Hex to Binary Convert each hex digit to four-bit binary

equivalent

BEEF16 = 1011 1110 1110 11112

Page 44: CMSC 150 data representation

Representing Different Information

So far, everything has been a number

What about characters? Punctuation?

Idea: put all the characters, punctuation in order assign a unique number to each done! (we know how to represent

numbers)

Page 45: CMSC 150 data representation

ASCII: American Standard Code for Information Interchange

Page 46: CMSC 150 data representation

'A' = 6510 = 010000012

'c' = 9910 = 011000112

'8' = 5610 = 001110002

ASCII: American Standard Code for Information Interchange

Page 47: CMSC 150 data representation

256 total characters…

How many bits needed?

ASCII: American Standard Code for Information Interchange

Page 48: CMSC 150 data representation

The Problem with ASCII What about Greek characters? Chinese?

UNICODE: use more bits UTF-8: use 1-4 eight-bit bytes 1,112,064 “code points” backward compatible w/ ASCII

How many characters could we represent w/ 32 bits? 232 = 4,294,967,296

Page 49: CMSC 150 data representation

You Control The Information What is this? 01001101

Page 50: CMSC 150 data representation

You Control The Information What is this? 01001101

Depends on how you interpret it:

010011012 = 7710 010011012 = 'M' 0100110110 = one million one thousand one hundred & one A machine-language instruction

You must be clear on representation and interpretation

Page 51: CMSC 150 data representation

Strings… "1000"

There are four characters '1' '0' '0' '0'

The binary representation is 00110001 00110000 00110000

00110000

100010 = 11111010002

Page 52: CMSC 150 data representation

Strings… "1000"

There are four characters '1' '0' '0' '0'

The binary representation is 00110001 00110000 00110000

00110000

100010 = 11111010002

“1234” ≠ 1234

Page 53: CMSC 150 data representation

One Last Thing… Real valued (AKA floating point) numbers?

Round-off error!

FLOPS: Floating Point Operations Per Second

Page 54: CMSC 150 data representation

One Last Thing… Real valued (AKA floating point) numbers?

Round-off error!

FLOPS: Floating Point Operations Per Second