Friday, August 13, 2010

,.,

Number System

 

Bit & Byte

Computer uses the binary system. Any physical system that can exist in two distinct states (e.g., 0-1, on-off, hi-lo, yes-no, up-down, north-south, etc.) has the potential of being used to represent numbers or characters. A binary digit is called a bit. There are two possible states in a bit, usually expressed as 0 and 1.
A series of eight bits strung together makes a byte, much as 12 makes a dozen. With 8 bits, or 8 binary digits, there exist 2^8=256 possible combinations. The following table shows some of these combinations. (The number enclosed in parentheses represents the decimal equivalent.)
00000000 (  0)   00010000 ( 16)   00100000 ( 32)  ...  01110000 (112)
     00000001 (  1)   00010001 ( 17)   00100001 ( 33)  ...  01110001 (113)
     00000010 (  2)   00010010 ( 18)   00100010 ( 34)  ...  01110010 (114)
     00000011 (  3)   00010011 ( 19)   00100011 ( 35)  ...  01110011 (115)
     00000100 (  4)   00010100 ( 20)   00100100 ( 36)  ...  01110100 (116)
     00000101 (  5)   00010101 ( 21)   00100101 ( 37)  ...  01110101 (117)
     00000110 (  6)   00010110 ( 22)   00100110 ( 38)  ...  01110110 (118)
     00000111 (  7)   00010111 ( 23)   00100111 ( 39)  ...  01110111 (119)
     00001000 (  8)   00011000 ( 24)   00101000 ( 40)  ...  01111000 (120)
     00001001 (  9)   00011001 ( 25)   00101001 ( 41)  ...  01111001 (121)
     00001010 ( 10)   00011010 ( 26)   00101010 ( 42)  ...  01111010 (122)
     00001011 ( 11)   00011011 ( 27)   00101011 ( 43)  ...  01111011 (123)
     00001100 ( 12)   00011100 ( 28)   00101100 ( 44)  ...  01111100 (124)
     00001101 ( 13)   00011101 ( 29)   00101101 ( 45)  ...  01111101 (125)
     00001110 ( 14)   00011110 ( 30)   00101110 ( 46)  ...  01111110 (126)
     00001111 ( 15)   00011111 ( 31)   00101111 ( 47)  ...  01111111 (127)

          :
     (continued)
          :

     10000000 (128)   10010000 (144)   10100000 (160)  ...  11110000 (240)
     10000001 (129)   10010001 (145)   10100001 (161)  ...  11110001 (241)
     10000010 (130)   10010010 (146)   10100010 (162)  ...  11110010 (242)
     10000011 (131)   10010011 (147)   10100011 (163)  ...  11110011 (243)
     10000100 (132)   10010100 (148)   10100100 (164)  ...  11110100 (244)
     10000101 (133)   10010101 (149)   10100101 (165)  ...  11110101 (245)
     10000110 (134)   10010110 (150)   10100110 (166)  ...  11110110 (246)
     10000111 (135)   10010111 (151)   10100111 (167)  ...  11110111 (247)
     10001000 (136)   10011000 (152)   10101000 (168)  ...  11111000 (248)
     10001001 (137)   10011001 (153)   10101001 (169)  ...  11111001 (249)
     10001010 (138)   10011010 (154)   10101010 (170)  ...  11111010 (250)
     10001011 (139)   10011011 (155)   10101011 (171)  ...  11111011 (251)
     10001100 (140)   10011100 (156)   10101100 (172)  ...  11111100 (252)
     10001101 (141)   10011101 (157)   10101101 (173)  ...  11111101 (253)
     10001110 (142)   10011110 (158)   10101110 (174)  ...  11111110 (254)
     10001111 (143)   10011111 (159)   10101111 (175)  ...  11111111 (255)


K & M

2^10=1024 is commonly referred to as a "K". It is approximately equal to one thousand. Thus, 1 Kbyte is 1024 bytes. Likewise, 1024K is referred to as a "Meg". It is approximately equal to a million. 1 Mega byte is 1024*1024=1,048,576 bytes. If you remember that 1 byte equals one alphabetical letter, you can develop a good feel for size.


Number System

You may regard each digit as a box that can hold a number. In the binary system, there can be only two choices for this number -- either a "0" or a "1". In the octal system, there can be eight possibilities:
"0", "1", "2", "3", "4", "5", "6", "7".
In the decimal system, there are ten different numbers that can enter the digit box:
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9".
In the hexadecimal system, we allow 16 numbers:
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", and "F".
As demonstrated by the following table, there is a direct correspondence between the binary system and the octal system, with three binary digits corresponding to one octal digit. Likewise, four binary digits translate directly into one hexadecimal digit. In computer usage, hexadecimal notation is especially common because it easily replaces the binary notation, which is too long and human mistakes in transcribing the binary numbers are too easily made. Base Conversion Table
BIN    OCT   HEX   DEC
     ----------------------
     0000   00     0     0
     0001   01     1     1
     0010   02     2     2
     0011   03     3     3
     0100   04     4     4
     0101   05     5     5
     0110   06     6     6
     0111   07     7     7
     ----------------------
     1000   10     8     8
     1001   11     9     9
     1010   12     A    10
     1011   13     B    11
     1100   14     C    12
     1101   15     D    13
     1110   16     E    14
     1111   17     F    15


Convert From Any Base To Decimal

Let's think more carefully what a decimal number means. For example, 1234 means that there are four boxes (digits); and there are 4 one's in the right-most box (least significant digit), 3 ten's in the next box, 2 hundred's in the next box, and finally 1 thousand's in the left-most box (most significant digit). The total is 1234:
Original Number:      1     2     3    4
                           |     |     |    |
     How Many Tokens:      1     2     3    4
     Digit/Token Value: 1000   100    10    1
     Value:             1000 + 200  + 30  + 4  = 1234

or simply,  1*1000 + 2*100 + 3*10 + 4*1 = 1234
Thus, each digit has a value: 10^0=1 for the least significant digit, increasing to 10^1=10, 10^2=100, 10^3=1000, and so forth. Likewise, the least significant digit in a hexadecimal number has a value of 16^0=1 for the least significant digit, increasing to 16^1=16 for the next digit, 16^2=256 for the next, 16^3=4096 for the next, and so forth. Thus, 1234 means that there are four boxes (digits); and there are 4 one's in the right-most box (least significant digit), 3 sixteen's in the next box, 2 256's in the next, and 1 4096's in the left-most box (most significant digit). The total is:
1*4096 + 2*256 + 3*16 + 4*1 = 4660
Example. Convert the hexadecimal number 4B3 to decimal notation. What about the decimal equivalent of the hexadecimal number 4B3.3?
Solution:
Original Number:     4    B    3  .  3
                          |    |    |     |
     How Many Tokens:     4   11    3     3
     Digit/Token Value: 256   16    1     0.0625
     Value:            1024 +176  + 3   + 0.1875  = 1203.1875
Example. Convert 234.14 expressed in an octal notation to decimal.
Solution:
Original Number:     2    3    4  .  1       4
                          |    |    |     |       |
     How Many Tokens:     2    3    4     1       4
     Digit/Token Value:  64    8    1     0.125   0.015625
     Value:             128 + 24  + 4   + 0.125 + 0.0625    = 156.1875
Another way is to think of a cash register with different slots, each holding bills of a different denomination.


Convert From Decimal to Any Base

Again, let's think about what you do to obtain each digit. As an example, let's start with a decimal number 1234 and convert it to decimal notation. To extract the last digit, you move the decimal point left by one digit, which means that you divide the given number by its base 10.
1234/10 = 123 + 4/10
The remainder of 4 is the last digit. To extract the next last digit, you again move the decimal point left by one digit and see what drops out.
123/10 = 12 + 3/10
The remainder of 3 is the next last digit. You repeat this process until there is nothing left. Then you stop. In summary, you do the following:
Quotient  Remainder
     -----------------------------
     1234/10 =   123        4 --------+
      123/10 =    12        3 ------+ |
       12/10 =     1        2 ----+ | |
        1/10 =     0        1 --+ | | |   (Stop when the quotient is 0.)
                                | | | |
                                1 2 3 4   (Base 10)
Now, let's try a nontrivial example. Let's express a decimal number 1341 in binary notation. Note that the desired base is 2, so we repeatedly divide the given decimal number by 2.
Quotient  Remainder
     -----------------------------
     1341/2  =   670        1 ----------------------+
      670/2  =   335        0 --------------------+ |
      335/2  =   167        1 ------------------+ | |
      167/2  =    83        1 ----------------+ | | |
       83/2  =    41        1 --------------+ | | | |
       41/2  =    20        1 ------------+ | | | | |
       20/2  =    10        0 ----------+ | | | | | |
       10/2  =     5        0 --------+ | | | | | | |
        5/2  =     2        1 ------+ | | | | | | | |
        2/2  =     1        0 ----+ | | | | | | | | |
        1/2  =     0        1 --+ | | | | | | | | | |  (Stop when the quotient is 0)
                                | | | | | | | | | | |
                                1 0 1 0 0 1 1 1 1 0 1  (BIN; Base 2)
Let's express the same decimal number 1341 in octal notation.
Quotient  Remainder
     -----------------------------
     1341/8  =   167        5 --------+
      167/8  =    20        7 ------+ |
       20/8  =     2        4 ----+ | |
        2/8  =     0        2 --+ | | |  (Stop when the quotient is 0)
                                | | | |
                                2 4 7 5  (OCT; Base 8)
Let's express the same decimal number 1341 in hexadecimal notation.
Quotient  Remainder
     -----------------------------
     1341/16 =    83       13 ------+
       83/16 =     5        3 ----+ |
        5/16 =     0        5 --+ | |  (Stop when the quotient is 0)
                                | | |
                                5 3 D  (HEX; Base 16)
Example. Convert the decimal number 3315 to hexadecimal notation. What about the hexadecimal equivalent of the decimal number 3315.3?
Solution:
               Quotient  Remainder
     -----------------------------
     3315/16 =   207        3 ------+
      207/16 =    12       15 ----+ |
       12/16 =     0       12 --+ | |  (Stop when the quotient is 0)
                                | | |
                                C F 3  (HEX; Base 16)

                                        (HEX; Base 16)
                Product  Integer Part   0.4 C C C ...
     --------------------------------     | | | |
     0.3*16   =   4.8        4        ----+ | | | | |
     0.8*16   =  12.8       12        ------+ | | | |
     0.8*16   =  12.8       12        --------+ | | |
     0.8*16   =  12.8       12        ----------+ | |
              :                       ---------------------+
              :
   Thus, 3315.3 (DEC) --> CF3.4CCC... (HEX)


Note that from the Base Conversion Table, you can easily get the binary notation from the hexadecimal number by grouping four binary digits per hexadecimal digit, or from or the octal number by grouping three binary digits per octal digit, and vice versa.
HEX  5    3    D
     BIN 0101 0011 1101

     OCT  2   4   7   5
     BIN 010 100 111 101
Finally, the fractional part is a decimal number can also be converted to any base by repeatedly multiplying the given number by the target base. Example: Convert a decimal number 0.1234 to binary notation
(BIN; Base 2)
                Product  Integer Part   0.0 0 0 1 1 1 1 1 1 0 0 1 ...
     --------------------------------     | | | | | | | | | | | | |
     0.1234*2 =  0.2468      0        ----+ | | | | | | | | | | | |
     0.2468*2 =  0.4936      0        ------+ | | | | | | | | | | |
     0.4936*2 =  0.9872      0        --------+ | | | | | | | | | |
     0.9872*2 =  1.9744      1        ----------+ | | | | | | | | |
     0.9744*2 =  1.9488      1        ------------+ | | | | | | | |
     0.9488*2 =  1.8976      1        --------------+ | | | | | | |
     0.8976*2 =  1.7952      1        ----------------+ | | | | | |
     0.7952*2 =  1.5904      1        ------------------+ | | | | |
     0.5904*2 =  1.1808      1        --------------------+ | | | |
     0.1808*2 =  0.3616      0        ----------------------+ | | |
     0.3616*2 =  0.7232      0        ------------------------+ | |
     0.7232*2 =  1.4464      1        --------------------------+ |
              :                       ----------------------------+
              :

Additon and Multiplication Tables

You generate the addition tables in bases other then 10 by following the same rule you do in base 10. The resulting tables have the appearance of shifting the columns to the left by one in each subsequent rows. Note how simple the addition and multiplication tables are for the binary system; addition operation is simply the bit-wise XOR operation with carry, and multiplication is simply the logical AND operation.
Decimal Addition Table:

   | 0  1  2  3  4  5  6  7  8  9
---+-----------------------------
 0 | 0  1  2  3  4  5  6  7  8  9
 1 | 1  2  3  4  5  6  7  8  9 10
 2 | 2  3  4  5  6  7  8  9 10 11
 3 | 3  4  5  6  7  8  9 10 11 12
 4 | 4  5  6  7  8  9 10 11 12 13
 5 | 5  6  7  8  9 10 11 12 13 14
 6 | 6  7  8  9 10 11 12 13 14 15
 7 | 7  8  9 10 11 12 13 14 15 16
 8 | 8  9 10 11 12 13 14 15 16 17
 9 | 9 10 11 12 13 14 15 16 17 18
Binary Addition Table:

   | 0  1
---+-----
 0 | 0  1
 1 | 1 10
Octal Addition Table:

   | 0  1  2  3  4  5  6  7
---+-----------------------
 0 | 0  1  2  3  4  5  6  7
 1 | 1  2  3  4  5  6  7 10
 2 | 2  3  4  5  6  7 10 11
 3 | 3  4  5  6  7 10 11 12
 4 | 4  5  6  7 10 11 12 13
 5 | 5  6  7 10 11 12 13 14
 6 | 6  7 10 11 12 13 14 15
 7 | 7 10 11 12 13 14 15 16
Hexadecimal Addition Table:

   | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
---+-----------------------------------------------
 0 | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
 1 | 1  2  3  4  5  6  7  8  9  A  B  C  D  E  F 10
 2 | 2  3  4  5  6  7  8  9  A  B  C  D  E  F 10 11
 3 | 3  4  5  6  7  8  9  A  B  C  D  E  F 10 11 12
 4 | 4  5  6  7  8  9  A  B  C  D  E  F 10 11 12 13
 5 | 5  6  7  8  9  A  B  C  D  E  F 10 11 12 13 14
 6 | 6  7  8  9  A  B  C  D  E  F 10 11 12 13 14 15
 7 | 7  8  9  A  B  C  D  E  F 10 11 12 13 14 15 16
 8 | 8  9  A  B  C  D  E  F 10 11 12 13 14 15 16 17
 9 | 9  A  B  C  D  E  F 10 11 12 13 14 15 16 17 18
 A | A  B  C  D  E  F 10 11 12 13 14 15 16 17 18 19
 B | B  C  D  E  F 10 11 12 13 14 15 16 17 18 19 1A
 C | C  D  E  F 10 11 12 13 14 15 16 17 18 19 1A 1B
 D | D  E  F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
 E | E  F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
 F | F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
You can also generate multiplication tables in bases other than 10 by following the same rule you do in base 10.
Decimal Multiplication Table:

   | 0  1  2  3  4  5  6  7  8  9
---+-----------------------------
 0 | 0  0  0  0  0  0  0  0  0  0
 1 | 0  1  2  3  4  5  6  7  8  9
 2 | 0  2  4  6  8 10 12 14 16 18
 3 | 0  3  6  9 12 15 18 21 24 27
 4 | 0  4  8 12 16 20 24 28 32 36
 5 | 0  5 10 15 20 25 30 35 40 45
 6 | 0  6 12 18 24 30 36 42 48 54
 7 | 0  7 14 21 28 35 42 49 56 63
 8 | 0  8 16 24 32 40 48 56 64 72
 9 | 0  9 18 27 36 45 54 63 72 81
Binary Multiplication Table:

   | 0  1
---+-----
 0 | 0  0
 1 | 0  1
Octal Multiplication Table:

   | 0  1  2  3  4  5  6  7
---+-----------------------
 0 | 0  0  0  0  0  0  0  0
 1 | 0  1  2  3  4  5  6  7
 2 | 0  2  4  6 10 12 14 16
 3 | 0  3  6 11 14 17 22 25
 4 | 0  4 10 14 20 24 30 34
 5 | 0  5 12 17 24 31 36 43
 6 | 0  6 14 22 30 36 44 52
 7 | 0  7 16 25 34 43 52 61
Hexadecimal Multiplication Table:

   | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
---+-----------------------------------------------
 0 | 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 1 | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
 2 | 0  2  4  6  8  A  C  E 10 12 14 16 18 1A 1C 1E
 3 | 0  3  6  9  C  F 12 15 18 1B 1E 21 24 27 2A 2D
 4 | 0  4  8  C 10 14 18 1C 20 24 28 2C 30 34 38 3C
 5 | 0  5  A  F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
 6 | 0  6  C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
 7 | 0  7  E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
 8 | 0  8 10 18 20 28 30 38 40 48 50 58 60 68 70 78
 9 | 0  9 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
 A | 0  A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
 B | 0  B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
 C | 0  C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
 D | 0  D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
 E | 0  E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
 F | 0  F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1


Arithmetic Operations

You do arithematic with hexadecimal numbers or numbers in any base in exactly the same way you do with decimal numbers, except that the addition and multiplcation tables you employ to base your calculations are a bit different. Substraction is equivalent to adding a negative number, and division is equivalent to multiplying by the inverse.
Example. Find the sum of two hexadecimal integers 123 and DEF.
Solution:
From the above hexadecimal addition table, we see that:
3+F=12, 2+E=10, and 1+D=E

             123
           + DEF
           -----
     carry   11
             E02
           -----
     sum     F12
Example. Find the product of two hexadecimal integers 123 and DEF.
Solution:
Step 1: We break down the second multiplier into single digits.
        123*DEF = 123*(D00+E0+F)
                = (123*D)*100 + (123*E)*10 + (123*F)

Step 2: We find the product in parentheses.
        From the above hexadecimal multiplication table, we see that:
        1*D=D, 2*D=1A, 3*D=27; thus,
        123*D = (100+20+3)*D
              = 1*D*100 + 2*D*10 + 3*D
              = D*100   + 1A*10  + 27
              = D00     + 1A0    + 27
              = EC7
        Likewise,
        123*E = (100+20+3)*E
              = 1*E*100 + 2*E*10 + 3*E
              = E*100   + 1C*10  + 2A
              = E00     + 1C0    + 2A
              = FEA
        123*F = (100+20+3)*F
              = 1*F*100 + 2*F*10 + 3*F
              = F*100   + 1E*10  + 2D
              = F00     + 1E0    + 2D
              = 110D

        Or, in elementary school style:

             123       123       123
           x   D     x   E     x   F
           -----     -----     -----
              27        2A        2D
             1A        1C        1E
             D         E         F
           -----     -----     -----
             EC7       FEA      110D

Step 3: We sum up the individual products.
        123*DEF = (123*D)*100 + (123*E)*10 + (123*F)
                = EC7*100     + FEA*10     + 110D
                = EC700       + FEA0       + 110D
                = FD6AD

        Or, in elementary school style:

             123
           x DEF
           -----
            110D
            FEA
           EC7
           -----
           FD6AD

No comments:

Post a Comment