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 10 0, the 10's place is 10 1, the 100's place is 10 2 and
the 1,000's place is 10 3.
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:
2,048 + 512 + 256 + 64 + 32 + 16 + 4 + 1 = 2,933.
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.
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:
16 0 = 1
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):
Now that we are familiar with the binary and hexadecimal number systems, we need to learn how to do
Binary and Hexdecimal Arithmetic.
©2002, 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.
2 0 = 1
2 1 = 2
2 2 = 4
2 3 = 8
2 4 = 16
2 5 = 32
2 6 = 64
2 7 = 128
2 8 = 256
2 9 = 512
2 10 = 1,024
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 0 2
(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 0 2 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 1 2:
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 10 3, 10 6, 10 9
and 10 12,
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:
2 1 0 0 5 2 1 4 2 1 5 2 6 3 1 8 4 2 6 8 4 2 6 8 4
2 1 - - - - - - - - - -
- - 1 0 1 1 0 1 1 1 0 1
0 1
Kilo = 2 10 = 1,024
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.
Mega = 2 20 = 1,048,576
Giga = 2 30 = 1,073,741,824
Tera = 2 40 = 1,099,511,627,776
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:
As an example we will convert decimal 763 into binary:
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - -
and subtract 512 from 763 to get 251.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1
and subtract 128 from 251 to get 123.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1
and subtract 64 from 123 to get 59.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1
and subtract 32 from 59 to get 27.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1 1
and subtract 16 from 27 to get 11.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1 1 1
and subtract 8 from 11 to get 3.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1 1 1 1
and subtract 2 from 3 to get 1.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1 1 1 1 1
and subtract 1 from 1 to get zero.
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 1 1 1 1 1 1 1
and find that decimal 763 is 1011111011 2
5 2 1 1 5 2 6 3 1 2 6 8 4 2 6 8 4 2 1 - - - - - - - - - - 1 0 1 1 1 1 1 0 1 1
Hexadecimal
It is clear from this example that binary numbers can get very large rather quickly. Consider the powers of
10 in binary:
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.
1 1 2 10 1010 2 100 1100100 2 1,000 1111101000 2 10,000 10011100010000 2 100,000 11000011010100000 2 1,000,000 11110100001001000000 2
0 16 = 0 0 0 0 2
It is worth noting that since 16 is 2 4, 16 3 is 2 12 and 16 4
is 2 16 (using ( x a ) b = x ( a * b ) with x = 2 and a = 4).
1 16 = 0 0 0 1 2
2 16 = 0 0 1 0 2
3 16 = 0 0 1 1 2
4 16 = 0 1 0 0 2
5 16 = 0 1 0 1 2
6 16 = 0 1 1 0 2
7 16 = 0 1 1 1 2
8 16 = 1 0 0 0 2
9 16 = 1 0 0 1 2
A 16 = 1 0 1 0 2
B 16 = 1 0 1 1 2
C 16 = 1 1 0 0 2
D 16 = 1 1 0 1 2
E 16 = 1 1 1 0 2
F 16 = 1 1 1 1 2
16 1 = 16
16 2 = 256
16 3 = 4,096
16 4 = 65,536
1 00012
1 16 10 10102
A 16 100 0110 01002
64 16 1,000 0011 1110 10002
3E8 16 10,000 0010 0111 0001 00002
2710 16 100,000 0001 1000 0110 1010 00002
186A0 16 1,000,000 1111 0100 0010 0100 00002
F4240 16
Decimal / Hexadecimal Conversions
Conversion from hexadecimal to decimal is straightforward, since the hexadecimal place values are consecutive
powers of 16. For example, 3E5 16 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:
As an example we will convert decimal 763 into hexadecimal:
You should convert this answer to both decimal and binary in order to verify that our algorithms produce
consistent results.
2 5 1 6 6 1 - - -
2 5 1 6 6 1 - - - 2
2 5 1 6 6 1 - - - 2 F
2 5 1 6 6 1 - - - 2 F B Go to: Title Page Table of Contents Index