CSC 110 – Intro to Computing Lecture 4: Arithmetic in other bases & Encoding Data
Announcements Copies of the slides are available on Blackboard and the course web page before and after each class Your slips selecting your service learning site are due next Tuesday
Announcements Homework #1 will be handed out today (and is on the course web page). It is due in my box by Monday at 4PM Quiz #1 on numerical systems will be given next Thursday in class
Review of Important Bases Binary (base-2) uses 2 digits: 0 & 1 Octal (base-8) uses 8 digits: 0, 1, 2, 3, 4, 5, 6, 7 Decimal (base-10) uses 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hexadecimal (base-16) uses 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Converting To Decimal We use positional notation to convert a number d n...d 3 d 2 d 1 d 0 from base-b to decimal: d 0 * b 0 d 1 * b 1 d 2 * b 2 d 3 * b 3 … + d n * b n
Converting From Decimal Converting a number from decimal to base-b While the decimal number is not 0 Divide the decimal number by b Move remainder to left end of answer Replace decimal number with quotient
Converting Binary To Octal Starting at the right, split into groupings of 3 bits For each group (working from right to left) Convert group from binary to decimal Add decimal number to left-end of solution
Converting Octal To Binary For each digit (working from right to left) Convert the digit from decimal to binary Add 0s to left of the binary to fill 3 bits Add binary number to left-end of solution Finally, remove any 0s padding leftmost digit
Binary to powers-of-two bases Conversion similar to binary ↔ octal Octal is base = 8 3 binary digits represent 1 octal digit Hexadecimal is base = 16 4 binary digits represent 1 hexadecimal digit When converting to hexadecimal, remember to use A, B, C, D, E, F and not 10, 11, 12, 13, 14, 15 for digits
Decimal Addition How do we add:
Decimal Addition How do we add: ones ones 6 ones
Decimal Addition How do we add: ones ones 66 ones
Decimal Addition How do we add: tens tens 612 tens
Decimal Addition How do we add: tens tens 6 1 hundred 2 tens
Decimal Addition How do we add: tens tens 26 1 hundred 2 tens
Decimal Addition How do we add: hundred hundreds hundreds thousand 0 hundred
Decimal Addition How do we add: thousand thousand thousand thousand
Decimal Addition How do we add:
Oops… I forgot one detail I forgot this was supposed to be in octal
Adding in Octal I forgot this was supposed to be in octal ones +3 ones 6 ones
Adding in Octal I forgot this was supposed to be in octal ones +3 ones 6 ones Whew = 6 8
Adding in Octal 8 1 = = 8 * 8 = eights +6 eights 12 eights What do we do now?
Adding in Octal 8 1 = = 8 * 8 = eights +6 eights 12 eights 8 eights = 1 sixty-four
Adding in Octal 8 1 = = 8 * 8 = eights +6 eights 12 eights –or– 12-8 = 4 eights and 1 sixty-four
Adding in Octal 8 1 = = 8 * 8 = eights +6 eights 4 eights and 1 sixty-four
Adding in Octal 8 2 = sixty-four 7 sixty-four +2 sixty-four 10 sixty-fours
Adding in Octal 8 2 = = 64 * 8 = sixty-four 7 sixty-four +2 sixty-four 10-8 = 2 sixty-fours 1 two fifty-six
Adding in Octal Finally, something makes sense again two fifty-six 5 two fifty-six +1 two fifty-six 7 two fity-six Yes! 7 is an octal digit
Adding in Octal That’s all, folks!
Algorithm For each position from right to left
Addition Algorithm To add 2 numbers in base-b: For each position from right to left Compute sum of the digits If sum > b then Record sum - b at position Carry the 1 If sum < b Record sum at position
Algorithm For each position from right to left
Algorithm Compute sum of the digits6 + 2 =
Algorithm If sum > b thenIf 8 > 8 then
Algorithm Record sum - b for position8 – 8 =
Algorithm Carry the one
Algorithm For each position from right to left
Algorithm Compute sum of digits =
Algorithm If sum < b then 5 <
Algorithm Record sum for position
Algorithm For each position from right to left
Algorithm Compute sum of digits1 + 7 =
Algorithm If sum > b then8 >
Algorithm Record sum – b for position8 – 8 =
Algorithm Carry the one
Algorithm For each position from right to left
Algorithm Compute sum of digits1 =
Algorithm If sum < b then 1 <
Algorithm Record sum for position
Algorithm We’re Done!
Addition Algorithm To add numbers in base-b: For each position from right to left Compute sum of the digits If sum > b then Record sum - b at positionCarry the 1 If sum < b Record sum at position
Data Encoding In the real world, data (“information”) is analog Falls along a infinite continuum Color changes when mixing paint Falling mercury levels as temperatures drop Hard to capture numerically Is it o F or o F right now? Do you even care about this difference?
Data Encoding Normally, we work with discrete numbers Round to nearest whole or rational number: 71 o F, 4.5 miles, 1 teaspoon Limits the amount of space needed Imagine if the morning news had to put on screen: o F or state the time as: 8:45: Computers follow this pattern It takes a lot of space to be precise!
Digitizing Data Computers work in binary (0-1) Good news: we know how to convert any number into binary! Suppose we have a CD containing pictures of my daughter, how do we turn the 0s and 1s on a CD into the colors that make up the cutest girl ever?
Color by Number Most pictures rely upon the RGB standard First 8 bits (“byte”) specify amount of red used Next byte specifies amount of green used Last byte specifies amount of blue used Mixing these numbers gives us many colors
Data Storage Storing data can require lots of space Each pixel (dot) in a color photo takes 4 bytes 5 megapixel (~million pixel) camera: 20MB per picture 32 pictures: 640MB (a CD holds 650MB)
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
Binary Representation How many colors can 8 bits generate? How many different colors can n bits represent?
For Next Lecture Have Chapter 4 started Be ready to discuss: Boolean logic AND, OR, XOR, NOT, NAND, NOR gates