Number Systems
60Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. A number or a character inside a computer is stored as a sequence of 0s and 1s. Each 0 or 1 is called a bit. The binary number system has two digits, 0 and 1.
Since we use decimal numbers in our daily life, binary numbers are not intuitive. When you write a number like 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.
You write programs using decimal number systems. However, if you write programs to deal with a system like an operating system, you need to use binary numbers to reach down to the "machine-level." Binary numbers tend to be very long and cumbersome. Hexadecimal numbers are often used to abbreviate binary numbers, with each hexadecimal digit representing exactly four binary digits. The hexadecimal number system has sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and 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 using a sequence of one or more of these digits. The value that each digit in the sequence represents depends on its position. A position in a sequence has a value that is 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:
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, the base of the binary number system is 2 since the binary number system has two digits, and the base of the hex number system is 16 since the hex number system has sixteen digits.
1.5.1. Conversions Between Binary Numbers and Decimal Numbers
Given a binary number bnbn-1bn-2… b2b1b0, the equivalent decimal value is
bn x 2n + bn-1 x 2n-1 + bn-2 x 2n-2 + ... + b2 x 22 + b1 x 21 + b0 x 20
The following are examples of converting binary numbers to decimals:
Binary Conversion Formula Decimal10 1 x 21 + 0 x 20 21000 1 x 23 + 0 x 22 + 0 x 21 + 0 x 20 810101011 1 x 27 + 0 x 26 + 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 171To 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 x 2n + bn-1 x 2n-1 + bn-2 x 2n-2 + ... + b2 x 22 + b1 x 21 + b0 x 20
These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders are b0, b1, b2, ..., bn-2, bn-1, and bn.
Conversions Between Hexadecimal Numbers and Decimal Numbers
Given a hexadecimal number hnhn-1hn-2 ... h2h1h0, the equivalent decimal value is
hn x 16n + hn-1 x 16n-1 + hn-2 x 16n-2 + ... + h2 x 162 + h1 x 161 + h0 x 160
The following are examples of converting hexadecimal numbers to decimals:
Hexadecimal Conversion Formula Decimal7F 7 x 161 + 15 x 160 127FFFF 15 x 163 + 15 x 162 + 15 x 161 + 15 x 160 65535431 4 x 162 + 3 x 161 + 1 x 160 1073To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits hn, hn-1, hn-2, ..., h2, h1, and h0 such that
d = hn x 16n + hn-1 x 16n-1 + hn-2 x 16n-2 + ... + h2 x 162 + h1 x 161 + h0 x 160
These numbers can be found by successively dividing d by 16 until the quotient is 0. The remainders are h0, h1, h2, ..., hn-2, hn-1, and hn.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows:
1.5.3. Conversions Between Binary Numbers and Hexadecimal Numbers
To convert a hexadecimal number to a binary number, simply convert each digit in the hexadecimal number into a four-digit binary number using
Table 1.1. Converting Hexadecimal to Binary Hexadecimal Binary Decimal0 0 01 1 12 10 23 11 34 100 45 101 56 110 67 111 78 1000 89 1001 9A 1010 10B 1011 11C 1100 12D 1101 13E 1110 14F 1111 15For 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.
PrintShare it! — Rate it: up down flag this hub






