Connecting with Computer Science, 2e Chapter 7 Numbering Systems and Data RepresentationsConnecting with Computer Science, 2e 2 Objectives • In this chapter you will: – Learn why numbering systems are important to understand – Refresh your knowledge of powers of numbers – Learn how numbering systems are used to count – Understand the significance of positional value in a numbering system – Learn the differences and similarities between numbering system basesConnecting with Computer Science, 2e 3 Objectives (cont’d.) • In this chapter you will (cont’d.): – Learn how to convert numbers between bases – Learn how to do binary and hexadecimal math – Learn how data is represented as binary in a computer – Learn how images and sounds are stored in a computerConnecting with Computer Science, 2e 4 Why You Need to Know About... Numbering Systems • Computers store programs and data as binary digits • Hexadecimal number system – Provides convenient representation – Written into error messages • Helpful as students interact with computers in later computing courses and throughout their careersConnecting with Computer Science, 2e 5 Powers of Numbers: A Refresher • Raising a number to a positive power (exponent) – Self-multiply number by the specified power – Example: 23 = 2 * 2 * 2 = 8 (asterisk = multiplication) – Special cases: 0 and 1 as powers • Any number raised to 0 = 1: 10,5550 = 1 • Any number raised to 1 = itself: 10,5551 = 10,555 • Raising a number to a negative power – Follow same steps for positive power – Divide result into one • Example: 2-3 = 1 / (23) = .125Counting Things • Numbers are used to count things – Base 10 (decimal): most familiar • Computer uses base 2 (binary) – Two unique digits: 0 and 1 • Base 16 (hexadecimal) represent binary digits – Sixteen unique digits: 0–9, A–F • Counting in all number systems – Count digits defined in number system until exhausted – Place 0 in ones column and carry 1 to the left Connecting with Computer Science, 2e 6Connecting with Computer Science, 2e 7 Positional Value • Key principle of numbering systems • Weight assigned to digit – Based on position in number • Steps for base 10 – Determine positional value of each digit by raising 10 to position within number • Radix point – Divides fractional portion from the whole portion of a numberConnecting with Computer Science, 2e 8 Positional Value (cont’d.) • Positional value example: 436.95 – 4 in position two – 3 in position one – 6 in position zero – To the right of the radix point: • 9 in position negative one • 5 in position negative twoConnecting with Computer Science, 2e 9 Positional Value (cont’d.) • Positional value specifies what multiplier the position gives to the overall number • Example: 4321 – Digit 3 multiplied by positional value of its position: 100 (102) – Sum of all digits multiplied by positional value determines total number of things being countedConnecting with Computer Science, 2e 10 Figure 7-1, Positional values for a base 10 number Positional Value (cont’d.)Connecting with Computer Science, 2e 11 Positional Value (cont’d.) • Positional value of the binary number 10112 – Rightmost position: positional value of the base (2) raised to the 0 power • Positional value: 1 – Next position: value of 2 raised to the power of 1 – Next position: 2 squared – Next position: 2 to the third • Positional value significance – Gives weight each digit contributes to the number’s overall valueConnecting with Computer Science, 2e 12 Figure 7-2, Positional values for a base 2 number Positional Value (cont’d.)Connecting with Computer Science, 2e 13 How Many Things Does a Number Represent? • Number equals sum of each digit x positional value – Translate with base 10 – Example: 10012 • Equivalent to nine things • (1 * 20) + (0 * 21) + (0 * 22) + (1 * 23) • 1 + 0 + 0 + 8 = 9 • General procedure (any base) – Calculate position value of the number by raising the base value to the power of the position – Multiply positional value by digit in that position – Add each calculated value togetherConverting Numbers Between Bases • Can represent any quantity in any base • Counting process similar for all bases – Count until highest digit for base reached – Add 1 to next higher position to left – Return to 0 in current position • Conversion: map from one base to another – Identities easily calculated – Identities obtained by table lookup Connecting with Computer Science, 2e 14Connecting with Computer Science, 2e 15 Table 7-1, Counting in different bases Converting Numbers Between Bases (cont’d.)Connecting with Computer Science, 2e 16 Converting to Base 10 • Methods: – Table lookup (more extensive than Table 7-1) – Algorithm for evaluating number in any base • Multiply the digit in each position by its positional value • Add results • Example: 1316 – Identify base: 16 – Map positions to digits – Raise, multiply, and add: • 1316 = (3 x 160) + (1 x 161) = 19Connecting with Computer Science, 2e 17 Converting from Base 10 • Method: – Reverse of converting to base 10 – Follow an algorithm for converting from base 10 • Algorithm – Determine target base positional value nearest to or equal in value to decimal number – Determine how many times that positional value can be divided into the decimal number – Multiply number from Step 2 by the associated positional value, and subtract product from number chosenConnecting with Computer Science, 2e 18 Converting from Base 10 (cont’d.) • Algorithm (cont’d.) – Use remainder from Step 3 as a new starting value, and repeat Steps 1-3 until Step 1 value is less than or equal to the maximum value in the target base – Converted number is the digits written down, in order from left to rightConnecting with Computer Science, 2e 19 Binary and Hexadecimal Math • Procedure for adding numbers similar in all bases – Difference lies in carry process – Value of carry equals value of base • Procedure for subtraction –
View Full Document