page 1 data structures in c for non-computer science majors kirs and pflughoeft basic data types...

10
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 92 4 Remainder 8 11 3 8 1 1 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

Upload: jasmine-chapman

Post on 23-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

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 9210 to Octal (base 8):

8 92 4

Remainder

8 11 3

8 1 1

0

Since the new quotient is 0, collect from last to first

9210 = 1 3 4 8

Check:

Number: 1 3 4 Position: 2 1 0

Value: 1* 82 + 3 * 81 + 4 * 80

= 1* 64 + 3 * 8 + 4 * 1 = 64 + 24 + 4 = 92

Octal/Hexadecimal

Page 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

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) ??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 ??Why is Octal easy ??Since Octal consists ONLY of the digits 0 through 7, ANY octal digit can be represented using only 3 bits:

Octal0

Binary000

1 001

2 010

3 011

Octal Binary4 1005 101

6 110

7 111

Page 3: 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

Page 3

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

Why is that easy?Why is that easy?

Consider the binary number 10111002

(which is 9210 and 1348 - 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.

Page 4: 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

Page 4

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

Example 2: Consider the number 45710

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

1110010012 = 45710

In Octal ??

111

7

001

1

001

1 = 7118

Sure??7118 = 7 * 82 + 1 * 81 + 1 * 80

= 7 * 64 + 1 * 8 + 1 * 1

= 448 + 8 + 1

= 457

Page 5: 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

Page 5

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

What about Hexadecimal (Base 16) ??What about Hexadecimal (Base 16) ??Basically, Hex is used for the same reason: It is easy to convert

Since 24 = 16 pieces of information, we can represent all the digits between 0 and 15:

Digit Binary Digit Binary

0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111

Page 6: 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

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. But 10, 11, 12, 13, 14, and 15 are NOT digits. They are Combinations of 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 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

Page 7: 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

Page 7

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

Once again, consider the binary number 10111002

= 9210 = 1348 - see the prior exampleWhat is the Hexadecimal Value ??

101 1100

5 C

Sure???

5C16 = 5 * 161 + C * 160

= 5C16

= 5 * 161 + 12 * 160

= 5 * 16 + 12 * 1

= 80 + 12 = 92

Page 8: 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

Page 8

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

Example 2: Consider the number 48610

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

1111001102 = 48610

In Hexadecimal ??

1

1

1110

E

0110

6 = 1E616

Sure??

1E616 = 1 * 162 + E * 161 + 6 * 160

= 1 * 162 + 14 * 161 + 6 * 160

= 256 + 224 + 6

= 486

= 1 * 256 + 14 * 16 + 6 * 1

Page 9: 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

Page 9

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Basic Data Types

What about Converting from Octal to Hexadecimal??What about Converting from Octal to Hexadecimal??The easiest way is to use binary numbers:

578 = 5 7

1 0 1 1 1 1

2 F = 2F16

Sure???1011112 = 25 + 23 + 22 + 21 + 20 = 32 + 8 + 4 + 2 + 1 = 4710

8 47 7

8 5 5

Octal

57

Hexadecimal

16 47

16 2

15 = F

2

2F

Page 10: 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

Page 10

Data Structures in C for Non-Computer Science Majors

Kirs and Pflughoeft

Repeat Slides for this Section

Go To Next Set of Slides For this Chapter

Go To Slide Index For Chapter 2

Go To Slide Index For Chapter 3

Go To Slide Index For Textbook

Go To Home Page

This Concludes The Slides for this Section

Choose an Option: