Download presentation
Presentation is loading. Please wait.
Published byHannah Powers Modified over 9 years ago
1
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 : 000 100 001101 010110 011111 And, with 4-bits, there are 16 possible combinations: 0000 010010001100 0001010110011101 0010011010101110 0011011111001111 Decimal To Binary
2
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 11 2 3 4 5 6 7 8 9 00 DecimalBinaryDecimalBinary 10 11 100 101 110 111 1000 1001 1010102010100 1110112110101 1211002210110 1311012310111 1411102411000 15 11112511001 16100002611010 17100012711011 18 100102811100 19100112911101
3
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: 95 95 Minutes Total 60 5752 5700 52 52 Seconds 1 1 Hour Total 60 95 60 35 35 Minutes So 5,752 Seconds = 1 hour, 35 minutes, and 52 seconds
4
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.
5
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 12 575 564 11 No. Inches Because there 3 Feet in a Yard: 15 No. Yards 3 47 45 2 No. Feet And so there are 15 yards, 2 feet, and 11 inches in 575 inches. Check: 3 * 15 = 45 + 2 47 * 12 = 564 + 11 = 575 Inches
6
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
7
Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Take, for example, the number 11 10 (which we know is 1011 2 ) 5 2 11 10 1 2 2 5 4 1 2 0 2 1 0 1 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 1011212 = 11 (Check the Table)
8
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) 0 2 14 0 2 7 1 2 3 1 2 1 1 The New Quotient is Zero: Quit Collecting Remainders from last to first: The Binary Value is: 11100202 Example 2: Convert 28 10 to binary (= 11100 2 ):
9
Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 3: Convert 44 10 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: 101100202
10
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:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.