Number Systems

advertisement
APPENDIX F
Number Systems
F.1 Introduction
Computers use binary numbers internally, because computers are made naturally to store and
process 0s and 1s. The binary number system has two digits, 0 and 1. A number or character
is stored as a sequence of 0s and 1s. Each 0 or 1 is called a bit (binary digit).
In our daily life we use decimal numbers. When we write a number such as 20 in a program, it is assumed to be a decimal number. Internally, computer software is used to convert
decimal numbers into binary numbers, and vice versa.
We write computer programs using decimal numbers. However, to deal with an operating
system, we need to reach down to the “machine level” by using binary numbers. Binary numbers tend to be very long and cumbersome. Often hexadecimal numbers are used to abbreviate
them, with each hexadecimal digit representing four binary digits. The hexadecimal number
system has 16 digits: 0–9 and A–F. The letters A, B, C, D, E, and F correspond to the decimal
numbers 10, 11, 12, 13, 14, and 15.
The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. A decimal
number is represented by a sequence of one or more of these digits. The value that each digit
represents depends on its position, which denotes an integral power of 10. For example, the
digits 7, 4, 2, and 3 in decimal number 7423 represent 7000, 400, 20, and 3, respectively, as
shown below:
7 4 2 3
binary numbers
decimal numbers
hexadecimal number
= 7 * 103 + 4 * 102 + 2 * 101 + 3 * 100
103 102 101 100 = 7000 + 400 + 20 + 3 = 7423
The decimal number system has ten digits, and the position values are integral powers of 10.
We say that 10 is the base or radix of the decimal number system. Similarly, since the binary
number system has two digits, its base is 2, and since the hex number system has 16 digits,
its base is 16.
If 1101 is a binary number, the digits 1, 1, 0, and 1 represent 1 * 23, 1 * 22, 0 * 21, and
1 * 20, respectively:
1 1 0 1
base
radix
= 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20
23 22 21 20 = 8 + 4 + 0 + 1 = 13
If 7423 is a hex number, the digits 7, 4, 2, and 3 represent 7 * 163, 4 * 162, 2 * 161, and
3 * 160, respectively:
7 4 2 3
= 7 * 163 + 4 * 162 + 2 * 161 + 3 * 160
163 162 161 160 = 28672 + 1024 + 32 + 3 = 29731
1273
1274 Appendix F
F.2 Conversions Between Binary and Decimal Numbers
binary to decimal
Given a binary number bnbn - 1bn - 2 c b2b1b0, the equivalent decimal value is
bn * 2n + bn - 1 * 2n - 1 + bn - 2 * 2n - 2 + c + b2 * 22 + b1 * 21 + b0 * 20
Here are some examples of converting binary numbers to decimals:
Binary
Conversion Formula
Decimal
10
1 * 21 + 0 * 20
2
1000
1 * 23 + 0 * 22 + 0 * 21 + 0 * 20
8
10101011
1 * 27 + 0 * 26 + 1 * 25 + 0 * 24 + 1 * 23 + 0 * 22 +
171
1 * 21 + 1 * 20
decimal to binary
To convert a decimal number d to a binary number is to find the bits bn, bn—1, bn—2, . . . , b2, b1
and b0 such that
d = bn * 2n + bn - 1 * 2n - 1 + bn - 2 * 2n - 2 + c + b2 * 22 + b1 * 21 + b0 * 20
These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders
are b0, b1, b2, c , bn - 2, bn - 1, and bn.
For example, the decimal number 123 is 1111011 in binary. The conversion is done as follows:
0
2
3
1
7
15
61
61
2 123
0
2
6
14
30
60
122
1
1
1
1
0
1
1
b6
b5
b4
b3
b2
b1
b0
1
2
3
2
7
2
15
2
30
30
2
Quotient
Remainder
Tip
The Windows Calculator, as shown in Figure F.1, is a useful tool for performing number
conversions. To run it, search for Calculator from the Start button and launch Calculator, then under View select Scientific.
Decimal
Binary
Hex
FIGURE F.1
You can perform number conversions using the Windows Calculator.
Appendix F 1275
F.3 Conversions Between Hexadecimal and Decimal
Numbers
Given a hexadecimal number hnhn - 1hn - 2 c h2h1h0, the equivalent decimal value is
n-1
hn * 16 + hn - 1 * 16
n
n-2
+ hn - 2 * 16
hex to decimal
+ c + h2 * 16 + h1 * 16 + h0 * 16
2
1
0
Here are some examples of converting hexadecimal numbers to decimals:
Hexadecimal
Conversion Formula
Decimal
7F
7 * 161 + 15 * 160
127
FFFF
15 * 163 + 15 * 162 + 15 * 161 + 15 * 160
65535
431
4 * 162 + 3 * 161 + 1 * 160
1073
To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits
hn, hn - 1, hn - 2, c , h2, h1, and h0 such that
decimal to hex
d = hn * 16n + hn - 1 * 16n - 1 + hn - 2 * 16n - 2 + c + h2 * 162
+ h1 * 161 + h0 * 160
These numbers can be found by successively dividing d by 16 until the quotient is 0. The
remainders are h0, h1, h2, c , hn - 2, hn - 1, and hn.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as
follows:
0
16
7
7
16
Quotient
123
0
112
7
11
h1
h0
Remainder
F.4 Conversions Between Binary and Hexadecimal
Numbers
To convert a hexadecimal to a binary number, simply convert each digit in the hexadecimal
number into a four-digit binary number, using Table F.1.
For example, the hexadecimal number 7B is 1111011, where 7 is 111 in binary, and B is
1011 in binary.
To convert a binary number to a hexadecimal, convert every four binary digits from right
to left in the binary number into a hexadecimal number.
For example, the binary number 1110001101 is 38D, since 1101 is D, 1000 is 8, and 11 is
3, as shown below.
1 1 1000 1101
3
8
D
hex to binary
binary to hex
1276 Appendix F
TABLE F.1 Converting Hexadecimal to Binary
Hexadecimal
Binary
Decimal
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
A
1010
10
B
1011
11
C
1100
12
D
1101
13
E
1110
14
F
1111
15
Note
Octal numbers are also useful. The octal number system has eight digits, 0 to 7. A decimal number 8 is represented in the octal system as 10.
Here are some good online resources for practicing number conversions:
■ http://forums.cisco.com/CertCom/game/binary_game_page.htm
■ http://people.sinclair.edu/nickreeder/Flash/binDec.htm
■ http://people.sinclair.edu/nickreeder/Flash/binHex.htm
✓
Check
Point
F.1
Convert the following decimal numbers into hexadecimal and binary numbers:
100; 4340; 2000
F.2
Convert the following binary numbers into hexadecimal and decimal numbers:
1000011001; 100000000; 100111
F.3
Convert the following hexadecimal numbers into binary and decimal numbers:
FEFA9; 93; 2000
Download