basic data types
DESCRIPTION
Decimal To Binary. 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. - PowerPoint PPT PresentationTRANSCRIPT
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 100001 101010 110011 111
And, with 4-bits, there are 16 possible combinations:
0000 0100 1000 11000001 0101 1001 11010010 0110 1010 11100011 0111 1100 1111
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
Decimal Binary
1 12
34
5
67
8
9
0 0
Decimal Binary Decimal Binary
1011
100
101
110111
1000
1001
10 1010 20 1010011 1011 21 1010112 1100 22 10110
13 1101 23 1011114 1110 24 11000
15 1111 25 11001
16 10000 26 1101017 10001 27 11011
18 10010 28 11100
19 10011 29 11101
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??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
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 Minutes Number 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
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
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 What does this have to do with converting from decimal to binary??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 yardsWe 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 1110 (which we know is 10112)
52 11 10 1
22 5 4 1
12 2 2 0
02 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
1 0 1 12 = 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)
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:
1 1 1 0 02
Example 2: Convert 2810 to binary (= 111002):
Page 9
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Basic Data Types
Example 3: Convert 4410 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 = 022/2 = 11 22 % 2 = 011/2 = 5 11 % 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:
1 0 1 1 0 02
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: