Binary and Hexadecimal Numbers

Information is represented electronically in a computer using voltages of different levels. For instance, a 0 could be represented by a 0 voltage and a 1 could be represented by a voltage of 3 volts. In principle, one could design a computer to use a large set of voltage levels; in practice, only two are used. This is because the closer the voltage levels are to each other, the greater the chance of distorting data due to voltage drift and noise. As an example, suppose that voltages which differ by 1 volt are used to represent consecutive integers. If the power supply output changes by .5 volts, it is impossible to determine whether a 2.5 volt level represents a 2 or a 3. In addition, as computers become faster, the voltage levels must be smaller to avoid overheating. As a result, we restrict ourselves to two voltage levels, which means that each piece of information in a computer can only represent the values 0 or 1.

Human beings commonly have 10 fingers, which has led to the adoption of the decimal (or base 10) number system as the common language of arithmetic. Just as in base 10 digits may take the values 0 through 9, in an arbitrary base "n" (where n is a positive integer), the digits may take values from 0 to "n-1"; hence in base 2 (the binary number system) the digits may take the values 0 or 1. For this reason, information in a computer is represented in binary, and each piece of information is called a bit (binary digit).

Consider the decimal number 4,263. Each digit in a number has what is commonly called a "place value." The 3 is in the "1's place", the 6 is in the "10's place", the 2 is in the "100's place" and the 4 is in the "1,000's place." Each of these place values in the decimal number system is a consecutive power of 10: the 1's place is 100, the 10's place is 101, the 100's place is 102 and the 1,000's place is 103.

In the binary number system, numbers larger than 1 require multiple bits, and each consecutive place value is a power of 2. You should memorize the following powers of 2, which will be useful both in this course and the subjects it is preparing you for:

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


Converting Binary to Decimal

Using these powers of 2, we can convert any binary number to decimal. For example, consider the binary number
1 1 0 1 02
(we will denote binary numbers using the subscript 2; numbers without subscripts will always be in decimal). Working from the right, which is called the least significant digit, we see that there is a 0 in the 1's place, a 1 in the 2's place, a 0 in the 4's place, a 1 in the 8's place and a 1 in the 16's place. Hence the decimal value of the binary number 1 1 0 1 02 is
16 + 8 + 2 = 26.
In converting a large binary number to decimal, it is often useful to write the place values above the bits, and simply add together all of the place values which have a 1. Since the place values are simply powers of 2, each consecutive place value is obtained by doubling the previous one. Suppose we wish to find the decimal equivalent of the binary number 1 0 1 1 0 1 1 1 0 1 0 12:
21
00521
42152631
8426842684 21
---------- --
1011011101 01

2,048 + 512 + 256 + 64 + 32 + 16 + 4 + 1 = 2,933.

Some large binary numbers have prefixes associated with them similar to the metric prefixes commonly used in the decimal number system. Unfortunately, the use of these prefixes is not entirely consistent. The metric prefixes "Kilo", "Mega", "Giga" and "Tera" stand for 103, 106, 109 and 1012, respectively. When referring to computer speed, these prefixes are interpreted to have their metric values. But when referring to amounts of memory or storage, these prefixes are usually interpreted to have the following binary values:
Kilo = 210 = 1,024
Mega = 220 = 1,048,576
Giga = 230 = 1,073,741,824
Tera = 240 = 1,099,511,627,776
Peta = 250 = 1,125,899,906,842,624
Exa = 260 = 1,152,921,504,606,846,976
Zetta = 270 = 1,180,591,620,717,411,303,424
Yotta = 280 = 1,208,925,819,614,629,174,706,176
Additional confusion arises when the vendors of computer equipment such as hard drives use metric prefixes instead of the more usual binary prefixes in order to make their product appear to have a larger capacity.



Converting Decimal to Binary

Conversion of a decimal number into binary is a little more complicated. Basically it is a matter of determining which places have 1's (since the remaining places will have 0's), and this can be accomplished by subtracting consecutive powers of 2, beginning at the left (the most significant digit). The following is an algorithm (a sequential set of operations designed to solve a problem) to convert a decimal number into binary:
  1. Identify the largest power of 2 which is less than or equal to the number.
  2. Write down all the binary place values from that power of 2 to 1.
  3. Put a 1 in the place corresponding to that power of 2.
  4. Subtract that power of 2 from the decimal number.
  5. Identify the largest power of 2 which is less than or equal to the new number.
  6. Repeat the last three steps until the new number is 0.
  7. Put 0's in all of the remaining places.
As an example we will convert decimal 763 into binary:
  1. Having memorized our powers of 2, we immediately recognize that the largest power of 2 which is less than or equal to 763 is 512.
  2. We then write down all the binary place values from 512 to 1:
    521
    152631
    2684268421
    ----------
  3. We put a 1 in the 512's place:
    521
    152631
    2684268421
    ----------
    1
    and subtract 512 from 763 to get 251.
  4. The largest power of 2 which is less than or equal to 251 is 128, so we put a 1 in the 128's place:
    521
    152631
    2684268421
    ----------
    1 1
    and subtract 128 from 251 to get 123.
  5. The largest power of 2 which is less than or equal to 123 is 64, so we put a 1 in the 64's place:
    521
    152631
    2684268421
    ----------
    1 11
    and subtract 64 from 123 to get 59.
  6. The largest power of 2 which is less than or equal to 59 is 32, so we put a 1 in the 32's place:
    521
    152631
    2684268421
    ----------
    1 111
    and subtract 32 from 59 to get 27.
  7. The largest power of 2 which is less than or equal to 27 is 16, so we put a 1 in the 16's place:
    521
    152631
    2684268421
    ----------
    1 1111
    and subtract 16 from 27 to get 11.
  8. The largest power of 2 which is less than or equal to 11 is 8, so we put a 1 in the 8's place:
    521
    152631
    2684268421
    ----------
    1 11111
    and subtract 8 from 11 to get 3.
  9. The largest power of 2 which is less than or equal to 3 is 2, so we put a 1 in the 2's place:
    521
    152631
    2684268421
    ----------
    1 11111 1
    and subtract 2 from 3 to get 1.
  10. The largest power of 2 which is less than or equal to 1 is 1, so we put a 1 in the 1's place:
    521
    152631
    2684268421
    ----------
    1 11111 11
    and subtract 1 from 1 to get zero.
  11. We now put 0's in the remaining places:
    521
    152631
    2684268421
    ----------
    1011111011
    and find that decimal 763 is 10111110112

This algorithm has the advantages of reinforcing your binary "numeracy" (numerical literacy), and since on the average, half of the digits in a binary number are 0, requiring fewer steps than other more mechanical algorithms. You should of course convert the binary result back into decimal as we did above, as a check that we did not make a mistake.



Hexadecimal

It is clear from this example that binary numbers can get very large rather quickly. Consider the powers of 10 in binary:
1 12
10 10102
100 11001002
1,000 11111010002
10,000 100111000100002
100,000 110000110101000002
1,000,000 111101000010010000002
The binary number system obviously poses problems for the ordinary human, yet it is the only way to represent numbers inside the computer. By choosing to work in a number system whose base is larger but still a power of 2, we can simplify numbers from a human point of view while retaining an accurate representation of what goes on inside the computer. Both octal (base 8) and hexadecimal (base 16) have been used in the past, but hexadecimal is far more common and will be a useful tool for us here.

Since hexadecimal is base 16, it must have single digits with values from 0 to 15. Hexadecimal digits representing values from 0 to 9 are obvious, and the letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15. You should memorize the binary values of all of the hexadecimal digits, and the decimal values of powers of 16 from 0 to 4:

016 = 0 0 0 02
116 = 0 0 0 12
216 = 0 0 1 02
316 = 0 0 1 12
416 = 0 1 0 02
516 = 0 1 0 12
616 = 0 1 1 02
716 = 0 1 1 12
816 = 1 0 0 02
916 = 1 0 0 12
A16 = 1 0 1 02
B16 = 1 0 1 12
C16 = 1 1 0 02
D16 = 1 1 0 12
E16 = 1 1 1 02
F16 = 1 1 1 12

160 = 1
161 = 16
162 = 256
163 = 4,096
164 = 65,536

It is worth noting that since 16 is 24, 163 is 212 and 164 is 216 (using ( xa )b = x( a * b ) with x = 2 and a = 4).

Conversion between binary and hexadecimal numbers is easy since we have memorized the hexadecimal digits in binary: we simply group the binary digits in groups of four, starting from the least significant bit, and translate into hexadecimal (inserting leading zeroes in front if we feel the need):

1
00012
116
10
10102
A16
100
0110  01002
6416
1,000
0011  1110  10002
3E816
10,000
0010  0111  0001  00002
271016
100,000
0001  1000  0110  1010  00002
186A016
1,000,000
1111  0100  0010  0100  00002
F424016


Decimal / Hexadecimal Conversions

Conversion from hexadecimal to decimal is straightforward, since the hexadecimal place values are consecutive powers of 16. For example, 3E516 is 3 * 256 + 14 * 16 + 5 = 997, since the 5 is in the 1's place, the E (with a decimal value of 14) is in the 16's place and the 3 is in the 256's place. Converting a decimal number to hexadecimal is accomplished using the following algorithm:
  1. Identify the largest power of 16 which is less than or equal to the number.
  2. Write down all the hexadecimal place values from that power of 16 to 1.
  3. Divide the number by that power of 16.
  4. Put the quotient in the place corresponding to that power of 16.
  5. Identify the largest power of 16 which is less than or equal to the remainder.
  6. Repeat the last three steps until the new number is 0.
  7. Put 0's in all of the remaining places.
As an example we will convert decimal 763 into hexadecimal:
  1. Having memorized our powers of 16, we immediately recognize that the largest power of 16 which is less than or equal to 763 is 256.
  2. We then write down all the hexadecimal place values from 256 to 1:
    2
    51
    661
    ---
  3. We divide 763 by 256 to get 2 with a remainder of 251, so we put a 2 in the 256's place:
    2
    51
    661
    ---
    2
  4. The largest power of 16 which is less than or equal to 251 is 16, so we divide 251 by 16 to get a quotient of 15 with the remainder of 11. We then put an F (hexadecimal for 15) in the 16's place:
    2
    51
    661
    ---
    2F
  5. The largest power of 16 which is less than or equal to 11 is 1, so we put a B (hexadecimal for 11) in the 1's place:
    2
    51
    661
    ---
    2FB
  6. Since all of the places have nonzero hexadecimal digits, the algorithm is finished and the answer is 2FB16.
You should convert this answer to both decimal and binary in order to verify that our algorithms produce consistent results.

Now that we are familiar with the binary and hexadecimal number systems, we need to learn how to do Binary and Hexdecimal Arithmetic.


Go to:Title PageTable of ContentsIndex

©2013, Kenneth R. Koehler. All Rights Reserved. This document may be freely reproduced provided that this copyright notice is included.

Please send comments or suggestions to the author.