Base Conversions Handout

advertisement
Bases, Number Systems, and Conversion between Numbering Systems:
Base 2 (Binary), Base 10 (Decimal) and Base 16 (Hexadecimal)
The base of a numbering system is the number of possible values each numerical symbol can have. In our
decimal numbering system, the numerical symbols are called digits. The position of each symbol tells us what
power of the base multiplies that symbol, because our base numbering system is a positional notation system.
Decimal (Base 10) Numbers:
Our normal numbering system is the decimal number system, which uses base 10. Base 10 tells us two things:
1. Each digit is an integer number from 0 to 9. There are 10 possible values for a digit, 0, 1, 2, …, 9.
2. Each digit is multiplied by a power of 10 depending on what position the digit is in.
Example 1: Decimal (base 10) numbers: Integers
The decimal number 4297 is an integer. To indicate a decimal (base 10) number, a subscript 10 can be applied
to the number: 429710. In general, any base B number can be written with a subscript B to indicate it has base
B. Normally we assume any number without a base subscript is a decimal number.
Digit position 1: Represents a number from 0 to 9. The number in the first digit position is multiplied by 1,
which is 100. A 7 in digit position 1 represents 7 x 100 =7.
Digit position 2: Represents a number from 10 to 90 (or 0 if that digit is 0). The number in the second digit
position is multiplied by 10, or 101. A 9 in digit position 2 represents 9 x 101 = 90.
Digit position 3: Represents a number from 100 to 900 (or 0 if that digit is 0). The number in the third digit
position is multiplied by 100, or 102. A 2 in digit position 3 represents 2 x 102 = 200.
Digit position 4: Represents a number from 1000 to 9000 (or 0 if that digit is 0). The number in the fourth digit
position is multiplied by 1000, or 103. A 4 in digit position 4 represents 4 x 103 = 4000.
The decimal number 429710 really represents (4 x 103) + (2 x 102) + (9 x 101) + (7 x 100). We understand this
and don’t normally need to expand decimal numbers in this fashion. Notice that each digit in position i is
multiplied by 10i-1, where position i=1 is the furthest right digit of the integer, indicating a number from 0 to 9.
What if the decimal number has a fractional portion, and isn’t just an integer?
Example 2: Decimal (base 10) numbers with a fractional portion
The decimal number 128.7510 has both an integer and a fractional portion. The decimal point separates the
integer portion of the number (128) from the fractional part of the number (75). Digits in the integer portion are
multiplied by positive powers of 10, according to their number position, just as in Example 2. Digits in the
fractional portion are multiplied by negative powers of 10. The digit just to the right of the decimal point is
multiplied by 10-1.
Digit position 1 to the right of the decimal point: The number in this digit position is multiplied by 10-1 or 1/10.
A 7 in this digit position represents the number 7 x 10-1 = 7/10 = 0.7.
Digit position 2 to the right of the decimal point: The number in this digit position is multiplied by 10-2 or
1/100. A 5 in this digit position represents the number 5 x 10-2 = 5/100 = 0.05.
The decimal number 128.7510 represents (1 x 102) + (2 x 101) + (8 x 100) + (7 x 10-1) + (5 x 10-2). Any decimal
number can be expanded in this fashion.
Now let’s consider a different base. Instead of base 10, let’s use base 2, for binary numbers.
Binary (Base 2) Numbers:
The symbols of binary numbers are called bits instead of digits.
1. Each bit is an integer number from 0 to 1; a bit only has 2 possible values, 0 or 1.
2. Each bit is multiplied by a power of 2, depending on its bit position in the number.
Binary numbers are used in digital logic because each bit of a binary number only have two values: 0 or 1. This
corresponds to an ‘off’ and an ‘on’ state, or a ‘low’ and a ‘high’ voltage. The logic gates used in this course,
and the transistors they are composed of, utilize these low and high voltage values to operate. Binary numbers
are the form of numbers recognized by computers, microprocessors, and the much simpler logic gates.
The number 11012 is a binary number; the subscript 2 tells us this is a base 2 number. Without the subscript, we
wouldn’t know whether this was a binary or decimal number, and would probably assume it to be a decimal
number. It’s important to indicate the number base on a number where the base might be ambiguous.
Example 3: Binary (base 2) numbers: Integers
The number 11012 is a binary integer number.
Bit position 1: Represents a number from 0 to 1. The number in the first bit position is multiplied by 1, which
is 20. A 1 in bit position 1 represents 1 x 20 = 1.
Bit position 2: Represents the number 2, or 0 if that bit is 0. The number in the second bit position is multiplied
by 21 = 2. A 1 in bit position 2 represents 1 x 21 = 2.
Bit position 3: Represents the number 4, or 0 if that bit is 0. The number in the third bit position is multiplied
by 22 = 4. A 0 in bit position 3 represents 0 x 22 = 0.
Bit position 4: Represents the number 8, or 0 if that bit is 0. The number in the fourth bit position is multiplied
by 23 = 8. A 1 in bit position 4 represents 1 x 23 = 8.
The binary number 11012 really represents (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20). Notice that each bit in
position i is multiplied by 2i-1, where position i=1 is the furthest right bit of the integer.
Expanding the binary number by powers of 2 allows us to easily convert a binary number into its equivalent
decimal number, as shown in Example 4 below.
Example 4: Converting from binary to decimal
The binary number 10112 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20), as shown above. Adding these powers of 2
together, we find that this number is equal to 8 + 0 + 2 + 1 = 1110. We have converted the binary number 10112
to the decimal number 1110.
Question 1: Converting from binary to decimal
What is 1001012 in decimal (base 10)?
Example 5: Binary (base 2) numbers with a fractional portion
Binary numbers can have fractional portions, just like decimal numbers. The binary number 101.112 has an
integer portion (1012) and fractional portion (112). Instead of a decimal point, the point separating the integer
portion from the fractional portion of the binary number is called the radix point. Radix is another name for
base; the term radix point is used to describe the separator between integer and fractional portion for any
number whose base is not 10 (that is, any non-decimal number).
Bit position 1 to the right of the radix point: The number in this bit position is multiplied by 2-1 = ½.
Bit position 2 to the right of the radix point: The number in this bit position is multiplied by 2-2 = ¼.
The binary number 101.112 represents (1 x 22) + (0 x 21) + (1 x 20) + (1 x 2-1) + (1 x 2-2). Expanding the binary
number into powers of two allows us to convert it into the equivalent decimal number, as shown in Example 6
below.
Example 6: Converting from binary to decimal (with fractional part)
The binary number 101.112 = (1 x 22) + (0 x 21) + (1 x 20) + (1 x 2-1) + (1 x 2-2), as shown above. Adding these
powers of 2 together, this binary number equals 4 + 0 + 1 + ½ + ¼ = (5 ¾ )10 = 5.7510 in decimal.
Question 2: Converting from binary to decimal (with fractional part)
What is 111.0112 in decimal?
Other bases may be used to represent numbers. Another numbering system common in digital systems is called
hexadecimal (base 16). Hexadecimal numbers are actually a shorthand representation of binary numbers.
Since 16 = 24, 4 bits can be represented as 1 hexadecimal (or ‘hex’, for short) number. A hexadecimal number
can be indicated by a subscript 16 at the end of the number (816). There are several other ways of indicating
hexadecimal numbers in various programming languages. Some microprocessor assembly languages use an H
(or h) at the end of the number (8H or 8h) to indicate a hexadecimal number. Our textbook uses H at the end of
the number to indicate a hexadecimal (base 16) number.
Hexadecimal (Base 16) Numbers:
The symbols of hexadecimal numbers are called digits or hex digits.
1. There are 16 possible hex digit symbols. The first 10 hex digits are the integers 0 through 9.
However, 10 cannot be used as a hex symbol because 10 is composed of two hex digits, a 1 and a 0,
and actually represents 1 x 161 + 0 x 160 = 16. So other symbols must be used for the last 6 hex
digits. The letters A through F are used to represent the integer values 10 through 15; 10 is
represented as A, 11 by B, 12 by C, 13 by D, 14 by E and 15 by F.
2. Each hex digit is multiplied by a power of 16, depending on its digit position in the hex number.
The number 2AC16 is a binary number; the subscript 16 tells us this is a base 16 or hexadecimal number.
Example 7: Hexadecimal (base 16) numbers: Integers
The hexadecimal number 2AC16 is an integer number.
Hex digit position 1: Represents a number from 0 to 15. The number in this position is multiplied by 160 = 1.
Hex digit position 2: The number in this position is multiplied by 161 = 16.
Hex digit position 3: The number in this position is multiplied by 162 = 256.
2AC16 = (2 x 162) + (A x 161) + (C X 160)
Example 8: Converting from hexadecimal to decimal
As shown above, 2AC16 = (2 x 162) + (A x 161) + (C X 160). But to convert this hexadecimal number to
decimal, we need to convert the hexadecimal symbols A16 and C16 into their decimal equivalents. A16
corresponds to a decimal 10, and C16 corresponds to a decimal 12. Thus we can calculate that
2AC16 = (2 x 256) + (10 x 16) + (12 x 1) = 51210 + 16010 + 1210 = 68410. We have converted the hexadecimal
number 2AC16 to its equivalent decimal number, 68410.
Converting from hexadecimal to binary
Another useful operation is to convert the hexadecimal number to its binary equivalent, which is how the
hexadecimal number is actually represented in microprocessors. Each hex digit corresponds to 4 bits. If we can
convert each hex digit into its corresponding 4-bit binary number, we can write the hex number as the combined
binary sequence of all the 4-bit binary numbers. To do this, we need to know what 4-bit binary number or
sequence corresponds to each hex digit.
Table 1 below lists each hex digit from 0 to F, and converts that digit to a 4-bit sequence.
Hex Digit
Decimal value
of Hex Digit
Bit 4 (20 = 1)
Bit 3 (20 = 1)
Bit 2 (20 = 1)
Bit 1 (20 = 1)
4-bit binary
sequence
0
0
0
0
0
0
0000
1
1
0
0
0
1
0001
2
2
0
0
1
0
0010
3
3
0
0
1
1
0011
4
4
0
1
0
0
0100
5
5
0
1
0
1
0101
6
6
0
1
1
0
0110
7
7
0
1
1
1
0111
8
8
1
0
0
0
1000
9
9
1
0
0
1
1001
A
10
1
0
1
0
1010
B
11
1
0
1
1
1011
C
12
1
1
0
0
1100
D
13
1
1
0
1
1101
E
14
1
1
1
0
1110
F
15
1
1
1
1
1111
Table 1: Hex digits and corresponding 4-bit binary sequence.
Example 9: Converting from hexadecimal to binary
The hex number 2AC16 can be converted to binary by reducing each hex digit to its 4-bit binary sequence. 216
corresponds to 00102, A16 corresponds to 10102 and C16 corresponds to 11002. Therefore, 2AC16 corresponds to
0010 1010 11002, where the spaces between each 4-bit group are simply to improve readability.
2AC16 = 0010 1010 11002
We may check that our binary expression for 2AC16 is correct by converting the binary number to a decimal
number: 0010 1010 11002 = 1 x 29 + 1 x 27 + 1 x 25 + 1 x 23 + 1 x 22 = 51210 + 12810 + 3210 + 810 + 410 = 68410.
This is the same answer as in example 8, which converted 2AC16 directly to decimal. Note that it was very
simple to convert the hexadecimal number to binary, and from there to a decimal number. You can always use
this procedure to convert a hexadecimal number (or any number with a base b that is a power of 2, b=2k) to a 4bit (or k-bit, for b=2k) binary number; that binary number can then be easily converted to a decimal value.
Question 3: Converting from hexadecimal to binary
What is D1F716 in binary?
Converting from binary to hexadecimal
We can convert a binary number into a hexadecimal number by separating the binary number into groups of 4
bits, starting from the radix point. If there are not enough bits to form all groups of 4, add extra zero bits so the
last group has 4 bits. Each 4-bit group is converted to a hex digit. The hexadecimal number is simply the set
of hex digits corresponding to each 4-bit group.
Example 10a: Converting from binary to hexadecimal (integer number)
The binary number 1011011101012 can be converted to hexadecimal as follows:
1. Form 4-bit groups: 1011 0111 01012
2. Convert each 4-bit group to a hex digit: 10112 = B16; 01112 = 716; 01012 = 516.
3. Combine hex digits to form hexadecimal number: B7516. So 1011011101012 = B7516.
Example 10b: Converting from binary to hexadecimal (integer number)
The binary number 11111010012 can be converted to hexadecimal as follows:
1. Form 4-bit groups: 11 1110 10012; add zeros so last group has 4 bits: 0011 1110 10012. Notice that
adding zeros to the front of the number does not change the value of the number, as it does not change
the bit position of the remaining bits relative to the radix point.
2. Convert each 4-bit group to a hex digit: 00112 = 316; 11102 = E16; 10012 = 916.
3. Combine hex digits to form hexadecimal number: 3E916.
Therefore, 11111010012 = 3E916.
Example 10c: Converting from binary to hexadecimal (fractional number)
The binary number 1001111.0110112 can be converted to hexadecimal as follows:
1. Form 4-bit groups, grouping to the left of the radix point and then to the right of the radix point.
a. Form 4-bit groups to the left of the radix point: 100 11112; add zeros to front of integer part to
make 4 bits in last group: 0100 11112.
b. Form 4-bit groups to the right of the radix point: 0110 112; add zeros to end of fractional part to
make 4 bits in last group: 0110 11002. Notice that adding zeros to the end of the fractional part
of the number does not change the value of the number, as it does not change the bit position of
the bits in the fractional portion relative to the radix point.
2. Convert each 4-bit group to a hex digit:
a. 0100 11112: 01002 = 416; 11112 = F16.
b. 0110 11002: 01102 = 616; 11002 = C16.
3. Combine hex digits to form hexadecimal number: 4F.6C16.
Notice that the position of the radix point stays the same, separating the integer portion of the hexadecimal
number from the fractional portion.
We find that 1001111.0110112 = 4F.6C16.
Question 4: Converting from binary to hexadecimal (fractional number)
What is 101010.111012 in hexadecimal (base 16)?
Converting from decimal to another base:
So far, we concentrated on converting from other bases, such as binary or hexadecimal, to decimal. However,
we may want to convert from decimal to binary, or to hexadecimal. Since we know how to easily convert from
binary to hexadecimal, I recommend that if you want to convert from decimal to hexadecimal, first convert the
decimal number to binary, and then convert the binary number to hexadecimal. This procedure, although
taking two steps, is much easier than converting directly from decimal to hexadecimal, which is rather tedious
and involves division of the decimal number by 16.
If the decimal number has both an integer and fractional portion, these portions must be converted to the other
base separately.
Converting from decimal to binary:
Integer Portion: There are two methods for converting the integer portion of a decimal number into an
integer binary number. These methods are 1) Sum of Powers of 2; and 2) Repeated Division by 2. Examples of
both methods are given. Method 2 (Repeated Division by 2) is easier if you have difficulty remembering all the
powers of 2; method 2 is also easier to implement as a computer program.
Method 1: Sum of Powers of 2 (for integers)
This method converts the decimal number to binary by representing the decimal number as a sum of numbers
that are powers of 2. All numbers can be represented in this manner, as 20 = 1. To determine what powers of 2
contribute to the sum, we begin by subtracting off the largest power of 2 that is less than or equal to the decimal
number. Then we subtract off the largest power of 2 that is less than or equal to the remainder, and so on, until
we obtain 0. This method is only used to convert the integer portion of the decimal number. Example 11
below illustrates the Sum of Powers of 2 method of converting a decimal number to binary.
Example 11: Converting decimal to binary using Sum of Powers of 2 method (for integers)
Convert the decimal number 10310 to binary:
1. The largest power of 2 less than or equal to 10310 is 6410 = 26. The next higher power of 2, 12810 =
27 is larger than 10310.
2. Subtract 6410 from 10310: 103 – 64 = 3910.
3. The largest power of 2 less than or equal to 3910 is 3210 = 25.
4. Subtract 3210 from 3910: 39 – 32 = 710.
5. The largest power of 2 less than or equal to 710 is 410 = 22.
6. Subtract 410 from 710: 7 – 4 = 310.
7. The largest power of 2 less than or equal to 310 is 210 = 21.
8. Subtract 210 from 310: 3 – 2 = 110.
9. The largest power of 2 less than or equal to 1 is 110 = 20.
10. Subtract 110 from 110: 1 – 1 = 010.
11. Now express 10310 as the sum of these powers of 2:
10310 = 6410 + 3210 + 410 + 210 + 110 = 26 + 25 + 22 + 21 + 20
12. Each power of 2 in the sum corresponds to a bit = 1: 26 means bit 7 = 1, 25 means bit 6 = 1, 22
means bit 2 = 1, etc. Any power of 2 not appearing in the sum corresponds to a bit = 0: no 24 means
bit 5 = 0, no 23 means bit 4 = 0. We write the binary number as a 7-bit number 11001112.
Using the Sum of Powers of 2 method, we find that 10310 = 11001112.
Question 5: Converting decimal to binary with Sum of Powers of 2 method (for integers)
What is 7810 in binary? Use the Sum of Powers of 2 method.
Method 2: Repeated Division by 2 (for integers)
This method converts the decimal number to binary by repeatedly dividing the decimal number by 2, until the
quotient equals 0. The binary number is found from the remainder after each division. The first remainder
(after the first division by 2) becomes the least significant bit (LSB) of the integer portion of the number, the bit
just to the left of the radix point. The last remainder becomes the most significant bit (MSB), the leftmost bit,
of the integer portion. This method is only used to find the binary number corresponding to the integer portion
of the decimal number.
Example 12: Converting decimal to binary using Repeated Division by 2 method (for integers)
Convert the decimal number 10310 to binary:
1. Divide 10310 by 2: 103/2 = 51 remainder 1  LSB is 1
2. Divide 5110 by 2: 51/2 = 25 remainder 1  next bit is 1
3. Divide 2510 by 2: 25/2 = 12 remainder 1  next bit is 1
4. Divide 1210 by 2: 12/2 = 6 remainder 0  0
5. Divide 610 by 2: 6/2 = 3 remainder 0  0
6. Divide 310 by 2: 3/2 = 1 remainder 1  1
7. Divide 110 by 2: 1/2 = 0 remainder 1  MSB is 1
8. Arrange the bits from MSB on left to LSB on right to obtain binary number:
11001112
10310 = 11001112. This is the same result as found using method 1, sum of powers of 2.
Question 6: Converting decimal to binary using Repeated Division by 2 method (for integers)
What is 7810 in binary? Use the Repeated Division by 2 method. You should get the same answer as for
Question 5.
Fractional portion of number: What if the decimal number is a real number, and has both an integer
portion and a fractional portion? We convert the integer portion of the decimal number into binary using either
the Sum of Powers of 2 Method or the Repeated Division by 2 Method. The fractional portion of the number
can be converted to binary using one of two different methods: either the Sum of Powers of 2 Method shown
earlier, or by the Repeated Multiplication by 2 Method (the inverse of the Repeated Division by 2 Method used
for integers).
Example 13: Converting decimal to binary fractional number using the Sum of Powers of 2 Method
Convert the decimal number 0.812510 to binary:
1. The largest power of 2 less than or equal to 0.812510 is 0.5 = ½ = 2-1. 20 = 1 > 0.5625 and is too large.
2. Subtract 0.510 from 0.812510: 0.8125 - 0.5 = 0.312510.
3. The largest power of 2 less than or equal to 0.312510 is 0.25 = ¼ = 2-2.
4. Subtract 0.2510 from 0.312510: 0.3125 – 0.25 = 0.062510.
5. The largest power of 2 less than or equal to 0.062510 is 0.0625 = 1/16 = 2-4.
6. Subtract 0.062510 from 0.062510: 0.0625 – 0.0625 = 010.
7. Write 0.062510 as the sum of these powers of 2: 0.062510 = 2-1 + 2-2 + 2-4 .
8. Now write the sum to include all powers of 2 from 2-1 to the smallest term in the sum (2-4). Each term
is multiplied by either 0 or 1. If the power term appears in the sum of step 7, multiply that power by 1;
if the term does not appear in the sum of step 7, multiply the power by 0.
0.062510 = 1 x 2-1 + 1 x 2-2 + 0 x 2-3 + 1 x 2-4.
9. The fractional binary number is formed from the sequence of 0s and 1s multiplying the powers of 2.
The multiplier of the 2-1 term is the first bit to the right of the binary point or radix point, and the bits
proceed in sequence to the right of that bit. The rightmost bit will be the multiplier of the smallest
power of 2, in this case the 2-4 term. We include the radix point or binary point, with the fractional
number to the right of the radix point. The integer part of the number goes to the left of the radix point.
In this case, the integer part of the number is 0, and is represented by a binary 0.
0.062510 = 0.11012.
Converting a fractional number from decimal to binary using the Sum of Powers of 2 works well if the number
is clearly a sum of powers of 2, and if you know many of the fractional powers of 2. If not, the Repeated
Multiplication by 2 Method discussed below is a better method to use.
Method 3: Repeated Multiplication by 2 Method (for fractional part of number)
The Repeated Multiplication by 2 method repeatedly multiplies the fraction by 2, until there is no fractional part
left. At each multiplication, the integer part of the resulting number (which will be either 0 or 1) is taken as the
bit value, starting from the MSB. Now multiply only the fractional part of the number by 2, discarding the
integer part of the number. This continues until either there is no fractional part left, or a repeating binary
pattern occurs, or the required number of fractional bits have been found (you may only have 8 bits to represent
the fractional portion of the number, for example). Note that it is possible to have a fractional number that
cannot be expressed as a finite number of fractional powers of 2; integer numbers, however, can always be
represented as a finite number of positive powers of 2.
Example 14: Converting decimal to binary (fractional part) using Repeated Multiplication by 2 Method
Convert the number 0.812510 to binary:
1. Multiply 0.812510 by 2: 0.8125 x 2 = 1.62510.
2. The integer part of 1.62510 is 1; the MSB (the bit just to the right of the radix or binary point, which
represents the 2-1 term) is 1.
3. Discard the 1 from 1.62510 and multiply 0.62510 by 2: 0.625 x 2 = 1.2510.
4. The integer part of 1.2510 is 1; the next bit to the right is therefore 1.
5. Discard the 1 from 1.2510 and multiply 0.2510 by 2: 0.25 x 2 = 0.510.
6. The integer part of 0.510 is 0; the next bit is 0.
7. Multiply 0.510 by 2: 0.5 x 2 = 1.010.
8. The integer part of 1.010 is 1; the LSB is 1.
9. The fractional part of 1.010 is 0.0. Stop at this point.
10. The fractional part of the binary number is formed from the sequence of bits from MSB to LSB, left to
right, with the MSB just to the right of the radix point. The integer part of the binary number goes to
the left of the radix point. For this decimal number, the integer part is 0, and so the binary integer
representation of this number is also 0.
0.812510 = 0.11012.
Question 6: Converting decimal to binary (fractional part) using Repeated Multiplication by 2 Method
What is 12.1562510 in binary? Use the Repeated Multiplication by 2 method to solve for the fractional portion,
and whatever method you prefer to solve for the integer part
.
Example 15: Converting decimal to binary (fractional part) using Repeated Multiplication by 2 Method
This example shows what to do when the decimal number is not a sum of powers of 2, and cannot be
represented as a finite number of bits.
Convert the number 0.410 to binary.
1. Multiply 0.410 by 2: 0.4 x 2 = 0.810.
2. The integer part of 0.810 is 0; the MSB of the fractional part of this number is 0.
3. Multiply 0.810 by 2: 0.8 x 2 = 1.610.
4. The integer part of 1.610 is 1; the next bit to the right is 1.
5. Discard the 1 from 1.610 and multiply 0.610 by 2: 0.6 x 2 = 1.210.
6. The next bit is 1.
7. Discard the 1 from 1.210 and multiply 0.210 by 2: 0.2 x 2 = 0.410.
8. The next bit is 0.
9. Multiply 0.410 by 2: 0.4 x 2 = 0.810.
10. At this point, we can see that the fraction is repeating. Since we obtained the same value of 0.8 in steps
9 and step 1, we’ll continue to cycle through these values 0.8, 1.6, 1.2, 0.4, 0.8, …, which give us a
repeating sequence of bits 0110… , represented with an overbar as 0110. We stop now, and
represent the fractional part of the number as 0110 in binary. The integer portion of the number is
0 in decimal, and represented as 0 in binary also.
0.410 = 0.01102.
Question 7: Converting decimal to binary (fractional part) using Repeated Multiplication by 2 Method
What is 0.210 in binary? Use the Repeated Multiplication by 2 Method. Stop when the binary fraction begins
repeating, and use the overbar notation to indicate what part of the fraction repeats.
Download