Download presentation
Presentation is loading. Please wait.
Published byJasmine Chapman Modified over 9 years ago
2
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Note that the methods we used to convert from decimal to binary, and back again, work for any base. If, for example we wished to convert 92 10 to Octal (base 8): 8 924 Remainder 8 113 8 11 0 Since the new quotient is 0, collect from last to first 92 10 =134 8 Check: Number: 1 3 4 Position: 2 1 0 Value: 1* 8 2 + 3 * 8 1 + 4 * 8 0 = 1* 64 + 3 * 8 + 4 * 1 = 64 + 24 + 4 = 92 Octal/Hexadecimal
3
Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Why would I care about octal (base 8) ?? As it turns out, Octal AND Hexadecimal (base 16) are often used because they allow for easy conversion to and from binary. Why is Octal easy ?? Since Octal consists ONLY of the digits 0 through 7, ANY octal digit can be represented using only 3 bits: Octal 0 Binary 000 1001 2010 3 011 OctalBinary 4100 5 101 6110 7111
4
Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Why is that easy? Consider the binary number 1011100 2 (which is 92 10 and 134 8 - see the previous example) 1011100 = 1 011 100 1 3 4 (Check against Table) Notice that there is a direct transference from binary to octal and octal to binary.
5
Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number 457 10 2 457 2 228 2 114 2 57 2 28 2 14 2 7 2 3 2 1 1 Stop and Collect 0 0 1 0 0 1 1 1 111001001212 = 457 10 In Octal ?? 111 7 001 1 1= 711 8 Sure?? 711 8 = 7 * 8 2 + 1 * 8 1 + 1 * 8080 = 7 * 64 + 1 * 8 + 1 * 1 = 448 + 8 + 1 = 457
6
Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What about Hexadecimal (Base 16) ?? Basically, Hex is used for the same reason: It is easy to convert Since 2 4 = 16 pieces of information, we can represent all the digits between 0 and 15: Digit Binary Digit Binary 0 0000 81000 1 0001 91001 2 0010101010 3 0011111011 40100121100 50101131101 60110141110 7 0111151111
7
Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types But 10, 11, 12, 13, 14, and 15 are NOT digits. They are Combinations of digits. True. We need to substitute the symbols: A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 The Conversion Table Should be: Digit Binary Digit Binary 0 000081000 1 000191001 2 0010A1010 3 0011B1011 40100C1100 50101D1101 60110E1110 7 0111F1111
8
Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Once again, consider the binary number 1011100 2 = 92 10 = 134 8 - see the prior example What is the Hexadecimal Value ?? 1011100 5C Sure??? 5C 16 = 5 * 16 1 + C * 16 0 = 5C 16 = 5 * 16 1 + 12 * 16 0 = 5 * 16 + 12 * 1 = 80 + 12 = 92
9
Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Example 2: Consider the number 486 10 2 486 2 243 2 121 2 60 2 30 2 15 2 7 2 3 2 1 0 Stop and Collect 1 1 0 0 1 1 1 1 111100110202 = 486 10 In Hexadecimal ?? 1 1 1110 E 0110 6= 1E6 16 Sure?? 1E6 16 = 1 * 16 2 + E * 16 1 + 6 * 16 0 = 1 * 16 2 + 14 * 16 1 + 6 * 16 0 = 256 + 224 + 6 = 486 = 1 * 256 + 14 * 16 + 6 * 1
10
Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What about Converting from Octal to Hexadecimal?? The easiest way is to use binary numbers: 57 8 = 5 7 1 0 11 1 1 2F= 2F 16 Sure??? 101111 2 = 25 25 + 23 23 + 22 22 + 2 1 + 2 0 = 32 + 8 + 4 + 2 + 1 = 47 10 8 477 8 55 Octal 57 Hexadecimal 16 47 16 2 15 = F 2 2F
11
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.