Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Remember when we first talked about the different combinations of ‘on’ and ‘off’ positions for a given number of light switches: With 3-bits, there are 8 possible combinations : And, with 4-bits, there are 16 possible combinations: Decimal To Binary
Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types The same sequencing pattern is used to represent numeric values DecimalBinary DecimalBinaryDecimalBinary
Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Is there any way of converting from decimal to binary?? Yes - The procedure is similar to one which we use all the time. Suppose you were asked to convert 5,752 seconds to Hours, Minutes, and Seconds. How would you do it???? Because there are 60 seconds to each minute: Because there are 60 minutes to each hour: Minutes Total Seconds 1 1 Hour Total Minutes So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds
Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Check: * 60 Seconds 5,700 Seconds + 52 Seconds 95 Minutes 5,752 Seconds 1 Hour * 60 = 60 MinutesNumber Hours Number Minutes Total Minutes Secs. Per Minute Number Seconds Total Seconds 35 Minutes Example #2: Convert 575 Inches to Yards, Feet, and Inches.
Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Because there 12 Inches in a Foot: 47 Total Feet No. Inches Because there 3 Feet in a Yard: 15 No. Yards No. Feet And so there are 15 yards, 2 feet, and 11 inches in 575 inches. Check: 3 * 15 = * 12 = = 575 Inches
Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What does this have to do with converting from decimal to binary?? for hours to minutes or minutes to seconds OR by 12 for inches to feet OR by 3 for feet to yards We divide by 2 for decimal to binary AND keep track of the remainders. Reconstructing the number from LAST to FIRST Instead of dividing by 60
Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Take, for example, the number (which we know is ) FIRST, Divide 11 by 2: NEXT, Let the quotient become the new Dividend CONTINUE Until the Quotient is Zero (0) FINALLY, Collect the remainders from Last to First = 11 (Check the Table)
Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types 2 28 (The notation used varies slightly to save space) Quotient Remainder (If Even = 0; If Odd = 1) The New Quotient is Zero: Quit Collecting Remainders from last to first: The Binary Value is: Example 2: Convert to binary (= ):
Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 3: Convert to binary: 44/2 = 22 The notation used differs to correspond to c syntax: / Integer Division: The result is the quotient % Modulus Arithmetic: The Result is the Remainder 44 % 2 = 0 22/2 = 1122 % 2 = 0 11/2 = 511 % 2 = 1 5/2 = 2 5 % 2 = 1 2/2 = 1 2 % 2 = 0 1/2 = 0 1 % 2 = 1 Since the New Quotient is 0: STOP Collecting from Last to First:
Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Repeat Slides for this SectionRepeat Slides for this Section Go To Next Set of Slides For this ChapterGo To Next Set of Slides For this Chapter Go To Slide Index For Chapter 2Go To Slide Index For Chapter 2 Go To Slide Index For Chapter 3Go To Slide Index For Chapter 3 Go To Slide Index For TextbookGo To Slide Index For Textbook Go To Home PageGo To Home Page This Concludes The Slides for this Section Choose an Option: