For obvious reasons, we understand the nature of numbers in base 10. While computers and data processing systems can implement numbers directly in base 10, it is generally not the most efficient or generalized implementation approach. Rather, binary numbers provide efficiencies that override their inconveniences. Dealing with these inconveniences is straight forward. In the last section, converting between non-negative integers and their binary equivalent.
Converting Between Non-Negative Decimal Integers and Unsigned Binary Numbers
In the previous section, the method to convert between non-negative decimal integers (0, 1, 2, …) and binary was presented. This method can be summarized by the following pseudocode:
Input: V Non-zero decimal number
Output: B String for unsigned binary number, leftmost index is 1
Temporary variable: T to hold intermediate results
Temporary variable: i to keep track of the place for the next bit
T=V;
i=0;
initialize all bits in B to ‘0’
while(T!=0)
// assume string B is sufficiently long
if( (T mod 2) == 1 ) B[length(B)-i]=’1′;
T=T/2 // integer division
i=i+1;
end while;
return B;
Language and application specific coding details are left to the reader.
Here is an example trace for the conversion of 20210 to binary 110010102
| i | T | T mod 2 | T / 2 | B |
|---|---|---|---|---|
| 0 | 202 | 0 | 101 | 00000000 |
| 1 | 101 | 1 | 50 | 00000010 |
| 2 | 50 | 0 | 25 | 00000010 |
| 3 | 25 | 1 | 12 | 00001010 |
| 4 | 12 | 0 | 6 | 00001010 |
| 5 | 6 | 0 | 3 | 00001010 |
| 6 | 3 | 1 | 1 | 01001010 |
| 7 | 1 | 1 | 0 | 11001010 |
Converting Between Unsigned Binary Numbers and Decimal
The decimal value for binary numbers can be computed using the power series formula provided earlier. In addition, an algorithm can be expressed to convert between binary and decimal.
Input: B String for unsigned binary number, leftmost index is 1
Output: V Non-zero decimal number
Temporary variable: i to keep track of the place for the next bit
V=0;
i=1;
while(i<=length(B))
V=2*V;
if( B[i]==’1′ ) V=V+1;
i=i+1;
end while;
return V;
Again, language and application specific coding details are left to the reader.
Here is an example trace for the conversion of 20210 to binary 110010102
| i | Vprev | B[i] | V |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 1 | 1 | 3 |
| 3 | 3 | 0 | 6 |
| 4 | 6 | 0 | 12 |
| 5 | 12 | 1 | 25 |
| 6 | 25 | 0 | 50 |
| 7 | 50 | 1 | 101 |
| 8 | 101 | 0 | 202 |
Conversions: General Case
Both of the algorithms above can be used to convert among cases in general. One needs only replace the value 2 with the desired base and expand the number of symbols for each digit. For example, 20210 can be shown to be 13025, where the subscript is the base. Note that the permissible base 5 digits are 0, 1, 2, 3, and 4. In digital systems, it is often convenient to express values in base 8 and 16. As will be shown shortly, because 8 and 16 are themselves powers of 2, a shortcut conversion between binary and these bases is useful. 20210=3128=CA16. The base 8 number looks reasonable and each digit can take on one of the values in 0, 1,…,7. The base 16 number looks really strange and the strangeness comes from the observation that we need 16 different symbols for digits. It is customary to use the first ten decimal digits, followed by the letters of the alphabet to fill out the 16 symbols. Hence, each hexadecimal digit can be one of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, E, or F.
Special Case Conversions Between Binary and Octal or Hexadecimal
If you already have a binary number, it turns out to be really easy to convert this value to either octal or hexadecimal. Let’s look first at previous binary value example 110010102. First, because 8=23, we will assign groups of three bits in the binary number to an octal digit. Before we do this, we need to make sure the number of bits in the binary number is a multiple of three. If it is we do nothing, if it is not, we prepend leading zeros until we reach a multiple of three. For the example value, we will need to prepend one 0 so that it now looks like 0110010102. Groups of three that are associated with octal digits are displayed in a common color. The three bit values are related to octal values in the following way:
| Binary code | Octal digit |
|---|---|
| 000 | 0 |
| 001 | 1 |
| 010 | 2 |
| 011 | 3 |
| 100 | 4 |
| 101 | 5 |
| 110 | 6 |
| 111 | 7 |
From the table, we can relate the binary codes to the octal digits and note that 0110010102=3128, which is the same value as noted previously.
A similar approach can be used to convert between binary and hexadecimal, except to use groupings of four bits. Because the original binary number is divisible by four, we don’t need to prepend any bits. We can similarly show that 110010102=CA16 using the following table.
| Binary code | Hexadecimal digit |
|---|---|
| 0000 | 0 |
| 0001 | 1 |
| 0010 | 2 |
| 0011 | 3 |
| 0100 | 4 |
| 0101 | 5 |
| 0110 | 6 |
| 0111 | 7 |
| 1000 | 8 |
| 1001 | 9 |
| 1010 | A |
| 1011 | B |
| 1100 | C |
| 1101 | D |
| 1110 | E |
| 1111 | F |