Number Conversions Between Decimal, Binary

advertisement
Gary Hecht
Number Conversions Between Decimal, Binary, and Hexadecimal
1. Example of converting a number from base 10 (“decimal”) to base 2 (“binary”):
11010 = ?2
Column weights for binary:
11010 =
•••
256
128
64
32
16
8
_0_
_0_
_1_
_1_
_0_ _1_
4
2
_1_
_1_
1
_0_2
Conversion steps:
Column weights in binary:
1. Start at a column weight greater than the
number to be converted
(e.g., start at a column weight of 128
for converting the base-ten value 110)
2. Can you ‘fit’ a 128 into the number to be
converted (110)? (No, so there is a ‘0’ in
the 128-weighted column)
3. Moving to the next lower column, can you ‘fit’ a 64
into the number to be converted (110)?
(Yes, so there is a ‘1’ in the 64-weighted column)
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
Since the 64-weighted bit is a ‘1’, subtract
64 from 110 to determine how much
remains to be expressed: 110 – 64 = 46
4. Moving to the next lower column, can you ‘fit’ a 32
into the remaining amount to be expressed (46)?
(Yes, so there is a ‘1’ in the 32-weighted column)
etc.
Since the 32-weighted bit is a ‘1’, subtract
32 from 46 to determine how much now
remains to be expressed: 46 – 32 = 14
5. Moving to the next lower column, can you ‘fit’ a 16
into the remaining amount to be expressed (14)?
(No, so there is a ‘0’ in the 16-weighted column)
6. Moving to the next lower column, can you ‘fit’ a 8 8. Moving to the next lower column, can
you ‘fit’ a 2 into the remaining amount
into the remaining amount to be expressed (14)?
to be expressed (2)?
(Yes, so there is a ‘1’ in the 8-weighted column)
(Yes, so there is a ‘1’ in the 2weighted column)
Since the 8-weighted bit is a ‘1’, subtract
8 from 14 to determine how much now
Since the 2-weighted bit is a ‘1’,
remains to be expressed: 14 – 8 = 6
subtract 2 from 2 to determine how
7. Moving to the next lower column, can you ‘fit’ a 4
much now remains to be expressed:
into the remaining amount to be expressed (6)?
2–2 = 0
(Yes, so there is a ‘1’ in the 4-weighted column)
9. Since the number is now fully
expressed, the remaining bits (just
Since the 4-weighted bit is a ‘1’, subtract
the 1-weighted bit for this example)
4 from 6 to determine how much now
are all ‘0’s’
remains to be expressed: 6 – 4 = 2
2
2. Example of converting a number from base 10 (“decimal”) to base 16 (“hexadecimal” or, simply,
“hex”):
11010 = ?16
Column weights for hex:
•••
11010 =
4096 256
16
1
_0_ _0_
_6_ _E_16 = 6Eh (the ‘h’ means ‘hex’)
Conversion steps:
Column weights in hex:
1. Start at a column weight greater than the number to
be converted (the entry in that column will be 0)
(e.g., start at a column weight of 256 for converting
the base-ten value 110)
2. Moving to the next lower column, how many 16’s can
‘fit’ into the number to be converted (110)? (6 16’s will
fit into 110 so place a 6 into the 16-weighted column)
160 = 1
161 = 16
162 = 256
163 = 4096
164 = 65,536
Now subtract 6•16 from 110 to determine how
etc.
much remains to be expressed: 110 – 6 • 16 = 14
3. Moving to the next lower column, how many 1’s can ‘fit’
into the remaining amount to be expressed (14)? (14 1’s
will fit into 14 so place an E into the 1-weighted column) (Note: E16 = 1410)
Now subtract 14•1 from the remaining amount to be
expressed (14) to determine how much now
remains to be expressed: 14 – 14 • 1 = 0
(and the number has been fully expressed)
An alternate conversion method:
1. Convert 110 (base 10) to binary:
11010 = _1_ _1_
_0_
_1_
(the conversion of 110 to binary
was illustrated in Example 1)
_1_
_1_
_0_2
2. Starting at the explicit, or implied, decimal point of the binary representation of the
number, move to the left creating groups of 4 bits:
11010 =
_1_
_1_
_0_
_1_
_1_
_1_
_0_2
3. Since each group of four bits is exactly equal to one hex digit – convert each group of
four bits to its hex equivalent (you may need to add one or more leading binary 0’s to the
highest-ordered group of bits to make a four-bit group).
(Note: Each group of four bits has bit weights 8/4/2/1 and can express 0 – 9, A - F)
11010 =
11010 =
0
_1_
_1_
6
_0_
_1_
_1_
_1_
E
_0_2
= 6E16 = 6Eh
3
Gary Hecht
Number of Values Expressible Using ‘n’ Digits
A given number of digits (n) in any given number base (B) can express the following number of
different values:
Number of different values expressible = Bn
Examples:
1. Two digits (n = 2) in base 10 (B = 10) can express 102 = 100 different values (e.g., 0 – 99)
2. Three digits (‘bits’) (n = 3) in base 2 (B = 2) can express 23 = 8 different values (e.g., 000, 001,
010, 011, 100, 101, 110, 111)
3. Four digits (‘bits’) (n = 4) in base 2 (B = 2) can express 24 = 16 different values (e.g., 0000 –
1111)
4. One digit (n = 1) in base 16 (B = 16) can express 161 = 16 different values (e.g., 0 – 9, A – F)
Note that four bits of binary (n = 4, B = 2) can express 16 different values which is the same
number of different values that can be expressed using one digit of hex (n = 1, B = 16). As such,
four bits of binary are equivalent to one hex digit.
4
Gary Hecht
Color Science (‘Colorimetry’)
‘True color’ typically uses 24 bits to define the color of any given pixel where the 24 bits are
divided as follows among the three ‘additive primaries’: 8 bits to define the strength of the red (R)
content, 8 bits to define the strength of the green (G) content, and 8 bits to define the strength of
the blue (B) content.
Since 4 bits exactly equals one hex digit, each 8-bit primary can be expressed as two hex digits
(00 – FF). HTML (hypertext markup language) code uses 6 hex digits to express the strength of
the RGB additive primaries when specifying the color of an object/text/etc. The six hex digits are
typically preceded with a ‘#’ symbol to indicate that the values are expressed in hex and the entire
value is within double quote marks. This is illustrated below:
Font color = “#RRGGBB”
where RR is the red strength (00 – FF), GG is the green strength
(00 – FF), and the BB is the blue strength (00 – FF).
Examples:
Font color = “#000000”
makes the font color black (no red, no green, no blue)
Font color = “#FFFFFF”
makes the font color full-brightness white (full-strength red, fullstrength green, full-strength blue)
Font color = “#7F7F7F”
makes the font color medium gray (half-strength red, half-strength
green, half-strength blue)
Font color = “#FF0000”
makes the font color bright red (full-strength red, no green, no
blue)
Font color = “#7F007F”
makes the font color medium-brightness magenta (half-strength
red, no green, half-strength blue)
Font color = “#FF8080”
makes the font color a de-saturated red (full-strength red, halfstrength green, half-strength blue)
(Note: A ‘de-saturated’ color is a pure color with white (or gray)
added – in this case consider FF8080 as:
RR = FF = 7F + 80, GG = 80, BB = 80 resulting in 7F units
of red added to 808080 (medium gray))
Download