Upload
steve-jones
View
1.537
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Number Base Conversions
Steven T. Jones
3/22/2010 1Copyright 2010 Steven T. Jones
General Rules
• The number base corresponds to the number of digits available
• Digits are positional, with each position representing a power of the base (more later)
• Base 10 (decimal) has ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
• Base 2 (binary) has two digits (0, 1)• Base 8 (octal) has eight digits (0, 1, 2, 3, 4, 5,
6, 7)• Base 6 (hexal – included only as an example)
has six digits (0, 1, 2, 3, 4, 5)
General Rules
• Base 16 (hexadecimal) has sixteen digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)– Note that the “letters” are actually digits
in base 16– A16 = 1010, B16 = 1110 ... F16 = 1510
–Why those particular “letters”? Because people familiar with the English alphabet understand that “A” comes before “B”, i.e. “A” < “B”
General Rules
• All number bases begin with 0• The largest digit in each base is one less
than the base– The largest digit in base 10 is 9– The largest digit in base 2 is 1– 7 in base 8; F (=1510) in base 16; 5 in base 6
• This means the base number is always represented by the digits “1” and “0”: 10– 102 = 210; 108 = 810; 1016 = 1610; 106 = 610
– See examples of counting, next slide
The significance of “10”
Counting in base 2
0 (= 010)0 + 1 = 1 (= 110)
1 + 1 = 10 (= 210)
Counting in base 80 (= 010)
0 + 1 = 1 (= 110)1 + 1 = 2 (= 210)2 + 1 = 3 (= 310)3 + 1 = 4 (= 410)4 + 1 = 5 (= 510)5 + 1 = 6 (= 610)6 + 1 = 7 (= 710)
7 + 1 = 10 (= 810)
Counting in base 100
0 + 1 = 11 + 1 = 22 + 1 = 33 + 1 = 44 + 1 = 55 + 1 = 66 + 1 = 77 + 1 = 88 + 1 = 9
9 + 1 = 10
Decimal is based on powers of 10
5
1000s
8 3 7
100s 10s 1s
7 X 1 =
3 X 10 =
8 X 100 =
5 X 1000 =
7
30
800
5000
583710
103 102 101 100
10000s
104
0 X 10000 = 0
...
(duh)
3/22/2010 6Copyright 2010 Steven T. Jones
Just for Fun: Hexal is based on powers of 6
3
216s
0 0 5
36s 6s 1s
5 X 1 =
0 X 6 =
0 X 36 =
3 X 216 =
5
0
0
648
583710
63 62 61 60
4
1296s
64
4 X 1296 = 5184
...
3/22/2010 7Copyright 2010 Steven T. Jones
Converting 583710 to Hexal
The first six powers of 6:60 = 1, 61 = 6, 62 = 36, 63 = 216, 64 = 1296, 65=77761. How many 7776s are there in 5837?
none2. How many 1296s are there in 5837? 4
1296 * 4 = 5184, 5837 - 5184 = 653 remaining 3. How many 216s are there in 653? 3
216 * 3 = 648, 653 - 648 = 5 remaining 4. How many 36s are there in 5? 05. How many 6s are there in 5? 06. Leaving 5 1s 5
583710 = 43,00563/22/2010 8Copyright 2010 Steven T. Jones
Binary is based on powers of 2
1
8s
0 0 1
4s 2s 1s
1 X 1 =
0 X 2 =
0 X 4 =
1 X 8 =
1
0
0
8
2510
23 22 21 20
1
16s
24
1 X 16 = 16
...
3/22/2010 9Copyright 2010 Steven T. Jones
Converting 2510 to Binary
The first six powers of 2:20 = 1, 21 = 2, 22 = 4, 23 = 8, 24 = 16, 25=321. How many 32s are there in 25? none2. How many 16s are there in 25? 1
16 * 1 = 16, 25 - 16 = 9 remaining 3. How many 8s are there in 9? 1
8 * 1 = 8, 9 - 8 = 1 remaining 4. How many 4s are there in 1? 05. How many 2s are there in 1? 06. Leaving 1 1 1
2510 = 1100123/22/2010 10Copyright 2010 Steven T. Jones
Octal is based on powers of 8
3
512s
3 5 3
64s 8s 1s
3 X 1 =
5 X 8 =
3 X 64 =
3 X 512 =
3
40
162
1536
583710
83 82 81 80
1
4096s
84
1 X 4096 = 4096
...
3/22/2010 11Copyright 2010 Steven T. Jones
Converting 583710 to Base8
The first six powers of 8:80 = 1, 81 = 8, 82 = 64, 83 = 512, 84 = 4096, 85=327681. How many 32768s are there in 5837? none2. How many 4096s are there in 5837? 1
4096 * 1 = 4096, 5837 - 4096 = 1741 remaining 3. How many 512s are there in 1741? 3
512 * 3 = 1536, 1741 - 1536 = 205 remaining 4. How many 64s are there in 205? 3
64 * 3 = 162, 205 - 162 = 43 remaining 5. How many 8s are there in 43? 5
8 * 5 = 40, 43 - 40 = 3 1s 3
5,83710 = 13,3538
3/22/2010 12Copyright 2010 Steven T. Jones
Hexadecimal is based on powers of 16
1
4096s
6 C D
256s 16s 1s
D X 1 =
C X 16 =
6 X 256 =
1 X 4096 =
13
192
1536
4096
583710
163 162 161 160
65536s
164
0 X 65536 = 0
...
3/22/2010 13Copyright 2010 Steven T. Jones
Converting 583710 to Hexadecimal
The first five powers of 16:160 = 1, 161 = 16, 162 = 256, 163 = 4096, 164 = 655361. How many 65536s are there in 5837? none2. How many 4096s are there in 5837? 1
4096 * 1 = 4096 , 5837 - 4096 = 1741 remaining 3. How many 256s are there in 1741? 6
256 * 6 = 1536, 1741 - 1536 = 205 remaining 4. How many 16s are there in 205? C (1210)
16 * 12 = 192, 205 - 192 = 13 1s D (1310)
583710 = 16CD16
3/22/2010 14Copyright 2010 Steven T. Jones
Bits and Nybbles
1 bit = 1 binary digit, possible values: 0, 1 (010 - 110)
2 bits = 2 binary digits, possible values:00, 01, 10, 11 (010 - 310)
3 bits = 3 binary digits, possible values: 000, 001, 010, 011, 100, 101, 110, 111 (010 - 710)
nybble (½ byte) = 4 bits = 4 binary digits, possible values: 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111(010 - 1510)
3/22/2010 15Copyright 2010 Steven T. Jones
Bytes and Words
1 byte = 8 binary digits, possible values: (010 - 25510)
2 bytes (standard C unsigned int) = 16 binary digits, possible values:
(010 - 32,76810)
4 bytes (standard C unsigned long) = 32 binary digits, possible values:
(010 - 2,147,483,64810)
For our purposes, a word is a unit of data made up of more than one byte
3/22/2010 16Copyright 2010 Steven T. Jones
Growing Binary Numbers into Hex (Bits 1-3)
One Bit, Two Combinations (0 and 1):0000 0001 010 110
Add a Red Bit (0 and 1) to Each Combination:0000 0001 0010 0011 010 110 210 310
Add a Blue Bit (0 and 1) to Each New Combination:0000 0001 0010 0011 0100 0101 0110 0111 010 110 210 310 410 510 610 710
3/22/2010 17Copyright 2010 Steven T. Jones
Growing Binary Numbers into Hex (Bit 4)
Add a Green Bit (0 and 1) to Each New Combination:0000 0001 0010 0011 0100 0101 0110 0111 010 110 210 310 410 510 610 710
016 116 216 316 416 516 616 716
1000 1001 1010 1011 1100 1101 1110 1111 810 910 1010 1110 1210 1310 1410 1510
816 916 A16 B16 C16 D16 E16 F16
• A hexadecimal digit can be thought of as a shorthand notation for a group of 4 bits (a “nybble”)
• Therefore, a byte (8 bits) can always be represented by 2 hex digits and a “standard” C integer (16 bits) can be represented by 4 hex digits (see next slide)
3/22/2010 18Copyright 2010 Steven T. Jones
Proving the Shorthand
Binary 00100010 =
216 216
Hexadecimal 22 =
0 X 128 = 00 X 64 =
01 X 32 = 32
0 X 16 = 0
0 X 8 = 0
0 X 4 = 0
1 X 2 = 2
0 X 1 = 0
34
2 X 16 = 322 X 1 = 2
34
01111101 = 0 X 128 = 0
716 D16 1 X 64 = 641 X 32 = 321 X 16 = 161 X 8 = 81 X 4 = 40 X 2 = 01 X 1 = 1
125
7D = 7 X 16 = 112D (13) X 1 = 13
1253/22/2010 19Copyright 2010 Steven T. Jones
Octal Shorthand for 6-Digit Binary Numbers
Binary 010010 =
28 28
Octal 22 =
0 X 32 = 01 X 16 = 16
0 X 8 = 00 X 4 = 01 X 2 = 20 X 1 = 0
18
2 X 8 = 162 X 1 = 2
18
101111 1 X 32 = 32 58 78 0 X 16 = 0
1 X 8 = 81 X 4 = 41 X 2 = 21 X 1 = 1
47
57 = 5 X 8 = 407 X 1 = 7
47
3/22/2010 20Copyright 2010 Steven T. Jones
Shorthand Notations Work Because ...
• Powers of 8 (1, 8, 64, 512, etc.) are also powers of 2– 1 = 20; 8 = 23; 64 = 26; etc.
• Powers of 16 (1, 16, 256, 4096, etc.) are also powers of 2– 1 = 20; 16 = 24; 256 = 28; etc.
• Every third power of 2 is a power of 8• Every fourth power of 2 is a power of 16• See next two slides
Exponential Correlations of 2 and 8
First 17 Powers of 2:20=1, 21=2, 22=4, 23=8, 24=16, 25=32, 26=64, 27=128, 28=256, 29=512, 210=1,024, 211=2,048, 212=4,096, 213=8,192, 214=16,384, 215=32,768, 216=65,536
First 6 Powers of 8:80=1,81=8,82=64,83=512,84=4,096,85=3
2,768 3/22/2010 22Copyright 2010 Steven T. Jones
Exponential Correlations of 2, 8, and 16
First 17 Powers of 2 (powers of 8 underscored):
20=1, 21=2, 22=4, 23=8, 24=16, 25=32, 26=64, 27=128,28=256, 29=512, 210=1,024, 211=2,048, 212=4,096, 213=8,192, 214=16,384,
215=32,768, 216=65,536
First 5 Powers of 16:160=1,161=16,162=256,163=4,096,164=65,536
3/22/2010 23Copyright 2010 Steven T. Jones
The Trouble with Decimals
• Computers are binary devices• The decimal values 0 and 1 can be
accurately and efficiently stored as binary numbers – as 0 and 1 (20)
• No other powers of 10 correspond to powers of 2, so ,unlike bases 2, 8, and 16, no direct equivalents exist
• Computers either store approximations of decimal numbers or they store them a digit at a time – see next slide
Storing Decimal Numbers Accurately
Value Binary Decimal 710 00000111 00000111
7 7 1410 00001110 00000001 00000100
14 1 4 4710 00101111 00000100 00000111
47 4 7 12810 1000000000000001 00000010 00001000
128 1 2 8
3/22/2010 25Copyright 2010 Steven T. Jones
http://www.kokomobeach.com