View Full Version : Numbering Systems
rVidia
10-22-2006, 09:51 AM
Note: Most of the work below is that of Randall Hyde's, author of "The Art of Assembly Language Programming"; however, I have cut and skipped over much of the text and have edited in some areas.
This thread will be organized as follows:
Post 2 (http://kickenhardware.net/forum/showthread.php?p=16581#post16581) - The decimal system (overview)
Post 3 (http://kickenhardware.net/forum/showthread.php?p=16582#post16582) - Converting binary to decimal
Post 4 (http://kickenhardware.net/forum/showthread.php?p=16584#post16584) - Converting decimal to binary
Post 5 (http://kickenhardware.net/forum/showthread.php?p=16585#post16585) - The binary system (notes)
Post 6 (http://kickenhardware.net/forum/showthread.php?p=16586#post16586) - About the hexadecimal system
Post 7 (http://kickenhardware.net/forum/showthread.php?p=16588#post16588) - Converting between binary/hex
rVidia
10-22-2006, 09:51 AM
You've been using the decimal (base 10) numbering system for so long that you probably take it for granted. When you see a number like "123", you don't think about the value 123; rather, you generate a mental image of how many items this value represents. In reality, however, the number 123 represents ("**" represents exponentiation)
1*10**2 + 2 * 10**1 + 3*10**0
or
100 + 20 + 3
Each digit that appears to the left of the decimal point represents a value between zero and nine, times an increasing power of ten. Digits appearing to the right of the decimal point represent a value between zero and nine, times an increasing negative power of ten. For example, the value 123.456 means
1*10**2 + 2*10**1 + 3*10**0 + 4*10**-1 + 5*10**-2 + 6*10**-3
or
100 + 20 + 3 + 0.4 + 0.05 + 0.006
rVidia
10-22-2006, 09:52 AM
The binary (base 2) numbering system works just like the decimal numbering system, with two exceptions: binary only allows the digits 0 and 1 (rather than 0-9), and binary uses powers of two rather than powers of ten. Therefore, it is rather easy to convert binary to decimal. For each "1" in the binary string, add in 2**n where "n" is the zero-based position of the binary digit. For example, the binary value 11001010 represents
1*2**7 + 1*2**6 + 0*2**5 + 0*2**4 + 1*2**3 + 0*2**2 + 1*2**1 + 0*2**0
=
128 + 64 + 8 + 2
=
202 (decimal)
rVidia
10-22-2006, 09:55 AM
Converting decimal to binary is slightly more difficult. You must find those powers of two that, when added together, produce the decimal result. The easiest method is to work from a large power of two down to 2**0. Consider the decimal value 1359:
Try: 2**10=1024, 2**11=2048.
1024 is the largest power of two that does not exceed 1359.
Subtract 1024 from 1359 and begin the binary value on the left with a "1" digit. Binary = "1", Decimal result is 1359 - 1024 = 335.
# The next lower power of two (2**9= 512) is greater than the result from above, so add a "0" to the end of the binary string. Binary = "10", Decimal result is still 335.
# The next lower power of two is 256 (2**8). Subtract this from 335 and add a "1" digit to the end of the binary number. Binary = "101", Decimal result is 79.
# 128 (2**7) is greater than 79, so tack a "0" on to the end of the binary string. Binary = "1010", Decimal result remains 79.
# The next lower power of two (2**6 = 64) is less than 79, so subtract 64 and append a "1" to the end of the binary string. Binary = "10101", Decimal result is 15.
# 15 is less than the next power of two (2**5 = 32), so simply add a "0" to the end of the binary string. Binary = "101010", Decimal result is still 15.
# 16 (2**4) is greater than the remainder, so append a "0" to the end of the binary string. Binary = "1010100", Decimal result is 15.
# 2**3 (eight) is less than 15, so stick another "1" digit onto the end of the binary string. Binary = "10101001", Decimal result is 7.
# 2**2 is less than seven, so subtract four from seven and append another one to the binary string. Binary = "101010011", decimal result is 3.
# 2**1 is less than three, so append a one to the end of the binary string and subtract two from the decimal value. Binary = "1010100111", Decimal result is now 1.
# Finally, the decimal result is one, which is 2**0, so add a final "1" to the end of the binary string. The final binary result is "10101001111"
rVidia
10-22-2006, 09:56 AM
In the purest sense, every binary number contains an infinite number of digits (or bits, which is short for binary digits). For example, we can represent the number five by
101 or 00000101 or 0000000000101 …, etc.
Any number of leading zero bits may precede the binary number without changing its value. We will adopt the convention of ignoring any leading zeros. For example, 101 (binary) represents the number five. Since the x86 architecture works with groups of eight, sixteen, or thirty-two bits, we'll find it much easier to zero-extend all binary numbers to some multiple of four or eight bits. Therefore, following this convention, we would represent the number five as 0101 (binary) or 00000101 (binary).
Additionally, in the United States, many people separate every three digits with a comma to make larger numbers easier to read. For example, 1,023,435,208 is much easier to read and comprehend than 1023435208. We'll adopt a similar convention for binary numbers. We will separate each group of four binary bits with a space. For example, the binary value 1010111110110010 will be written as 1010 1111 1011 0010.
rVidia
10-22-2006, 09:57 AM
A big problem with the binary system is verbosity. Representing the value 202 (decimal) requires eight binary digits. The decimal version requires only three decimal digits and, thus, represents numbers much more compactly than the binary numbering system. When dealing with large values, binary numbers quickly become too unwieldy. Unfortunately, the computer thinks in binary, so most of the time it is convenient to use the binary numbering system. Although we can convert between decimal and binary, the conversion is not a trivial task. The hexadecimal (base 16) numbering system solves these problems. Hexadecimal numbers offer the two features we're looking for: they're very compact, and it's simple to convert them to binary and vice versa. Because of this, most binary computer systems today use the hexadecimal numbering system. Since the radix (base) of a hexadecimal number is 16, each hexadecimal digit to the left of the hexadecimal point represents some value, times a successive power of 16. For example, the number 1234 (hexadecimal) is equal to
1*16**3 + 2*16**2 + 3*16**1 + 4*16**0
or
4096 + 512 + 48 + 4 = 4660 (decimal)
Each hexadecimal digit can represent one of sixteen values between 0 and 15. Since there are only ten decimal digits, we need to invent six additional digits to represent the values in the range 10 through 15. Rather than create new symbols for these digits, we'll use the letters A through F. The following are all examples of valid hexadecimal numbers:
1234 DEAD BEEF 0AFB FEED DEAF
Since we'll often need to enter hexadecimal numbers into the computer system, we'll need a different mechanism for representing hexadecimal numbers. We'll adopt the following conventions:
# All numeric values (regardless of their radix) begin with a decimal digit.
# All hexadecimal values end with the letter "h"; e.g., 123A4h.
# All binary values end with the letter "b".
# Decimal numbers may have a "t" or "d" suffix.
Examples of valid hexadecimal numbers:
1234h 0DEADh 0BEEFh 0AFBh 0FEEDh 0DEAFh
rVidia
10-22-2006, 09:58 AM
As you can see, hexadecimal numbers are compact and easy to read. Additionally, you can easily convert between hexadecimal and binary. Consider the following conversions:
Binary/Hex Conversions
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
To convert a hexadecimal number into a binary number, simply substitute the corresponding four bits for each hexadecimal digit in the number. For example, to convert 0ABCDh into a binary value, simply convert each hexadecimal digit according to the conversion chart above:
0 A B C D Hexadecimal
0000 1010 1011 1100 1101 Binary
To convert a binary number into hexadecimal format is almost as easy. The first step is to pad the binary number with zeros to ensure there is a multiple of four bits in the number. For example, given the binary number 1011001010, the first step would be to add two bits to the left of the number so that it contains 12 bits. The converted binary value is 001011001010. The next step is to separate the binary value into groups of four bits; e.g., 0010 1100 1010. Finally, look up these binary values in the conversion chart and substitute them for their appropriate hexadecimal digits.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.