csc 110 – intro to computing lecture 3: converting between bases & arithmetic in other bases

36
CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Post on 21-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

CSC 110 –Intro to Computing

Lecture 3:

Converting between bases & Arithmetic in other bases

Page 2: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Announcements

Copies of the slides are available on Blackboard and the course web page before and after each class

The end is near! All drop/add slips must be turned in by Sept. 7th.

I have a cool office. Please stop by and look (you could also me questions you have at the same time).

Page 3: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting binary to decimal

Binary positional notationdn * 2n-1 + dn-1 * 2n-2 + … + d2 * 21 + d1 * 20

What is binary 1101 in decimal?d4 = 1 d3 = 1 d2 = 0 d1 = 1

1101 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20

= 8 + 4 + 0 + 1 = 13

Page 4: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting octal to decimal

Octal positional notationdn * 8n-1 + dn-1 * 8n-2 + … + d2 * 81 + d1 * 80

What is octal 126 in decimald3 = 1 d2 = 2 d1 = 6

126 = 1 * 82 + 2 * 81 + 6 * 80

= 64 + 16 + 6= 86

Page 5: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Self-Quiz

What is the decimal number equal to:Binary 1110?

Binary 100101?

Octal 31?

Octal 17?

Page 6: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Base 16

Also known as hexadecimal Uses 10 expected digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Still needs 6 more; by convention we use:

A, B, C, D, E, FA == “10”, B == “11”, C == “12”, …, F == “15”

Page 7: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting Hex to Decimal

Equation converting base 16 to base 10:

What is the decimal value of hex FACE?

Page 8: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Powers-of-Two Conversion

Converting between binary and octal or hex is easyWhy?

How many bits per octal digit?

How many bits per nibble (hex digit)?

Page 9: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting Binary to Hex

What is 1111111011101101 in hex?

What is 1111101011001110 in hex?

Page 10: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting Hex to Binary

What is 65D3 in binary?

What is BEEF is binary?

Page 11: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting Octal and Binary

What is octal 6743 in binary?

What is binary 101110100010001 in octal?

Page 12: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting from Base 10

Early programmers wrote programs entirely in binaryDecimal notation still rare today. Why?

Page 13: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting from Base 10

Conversion follows simple algorithm:Do

Divide decimal number by new base

Next left-most digit is remainder from division

Replace decimal number with division quotient

Repeat until decimal number is zero

Page 14: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Convert Decimal to Binary

What is 10 (decimal) in binary?

Page 15: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Self-Test

Convert into from decimal to binary:14

63

Page 16: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Convert Decimal to Hex

Convert 48813 from decimal to hex

Page 17: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Converting to/from decimal?

Who is ready for this on midterm?What questions do you have?

If you are uncomfortable asking in class:Ask anyway (others have same questions)!Ask me in my office hoursPost anonymously on blackboardTry exercises 28 – 34 in book (p. 48-49)

Page 18: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Addition Refresher

How do we add two numbers together?

71262331

589311

Page 19: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Adding in other bases

Rules are very synonmousCarry the one when above value of baseFor instance in base 2: 1

+ 1 10

Or in base 8: 4+ 4 10

Page 20: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Adding binary numbers

101010 + 110001

111110+ 11

Page 21: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Adding hexadecimal numbers

FEED+FACE

BEEF+ EA7

Page 22: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Subtraction Refresher

Subtraction is base 10:

7213- 6999

Page 23: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Subtraction in other bases

Rules are very synonmousBorrow the value of base when necessaryFor instance in base 2: 2

10- 1 1

Or in base 8: 16

- 7 7

Page 24: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Subtracting octal numbers

7535- 3536

4562- 777

Page 25: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Subtracting binary numbers

10110110- 1010001

10000000- 1111111

Page 26: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Data Encoding

Data (“information”) is traditionally encoded in analog formatsFalls along a continuum with lots of minimal

changes Color changes when mixing paint Rising mercury levels when temperature increases

Easy for nature, but hard to capture numerically How to capture precision: Is it 71.848174oF or

71.848173oF?

Page 27: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Encoding Data

Easier to encode discrete dataE.g., Using integer or rational numbers

71oF or 4.5 miles.Also bounds space needed to record data

For this reason, computers only use discrete data

Page 28: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Digitizing Data

Computers work in binary (0-1)Makes computing cheaper and simplerLimited loss of precision:

Can convert all integers into binary

How does this work?

Page 29: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Digitizing Data

Figure 3.3

Signals in this region considered 0

Signals in this region considered 1

How digital data is captured and processed

Page 30: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Binary Representation

1 bit captures 2 states: 0 or 1 2 bits captures 4 states: 00, 01, 10, 11 3 bits capture 8 states: 000, 001, 010, 011,

100, 101, 110, 111

Page 31: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Binary Representation

How many states can 4 bits capture?

How many different states can n bits represent?

Page 32: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Data Storage

Storing data can require lots of spaceEach pixel (dot) in a color photo takes 4 bytes5 megapixel (~million pixel) camera:

20MB per picture32 pictures: 640MB (a CD holds 650MB)

Page 33: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Compression

Much of this data is repetitive or unneededAreas in pictures contain similar data

Pixels of clothing, leaves, or the sky will be similar

Music contains lots of sounds we cannot hear

Compression limits the space data uses

Page 34: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

2 types of compression

Lossless compressionLmt spce tkn w/o losing data Important when all data is important

E.g., bank records, grade reports, census data

Lossy compressionNo(table) because data is lost in compressionUseful when not all data is important

E.g. mp3 files remove sounds people cannot hear

Page 35: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

Compression Ratios

Another way to compare algorithms is their compression rateMeasures how well data are compressedExpressed as a value between 0% and 100%

0% perfect compression 100% no compression Most algorithms lie somewhere in between

Page 36: CSC 110 – Intro to Computing Lecture 3: Converting between bases & Arithmetic in other bases

For Next Lecture

Have Chapter 3 finished Be ready to discuss:

Hexadecimal numbersConverting from decimal to other basesArithmetic in bases other than 10