Page 29 - ARM 64 Bit Assembly Language
P. 29

12 Chapter 1

                                   Base 2  Base 16
                                    0000      0
                                    0001      1
                                    0010      2
                                    0011      3                      Base 2  Base 8
                                    0100      4                       000      0
                                    0101      5                       001      1
                                    0110      6                       010      2
                                    0111      7                       011      3
                                    1000      8                       100      4
                                    1001      9                       101      5
                                    1010      A                       110      6
                                    1011      B                       111      7
                                    1100      C
                                    1101      D
                                    1110      E
                                    1111      F

                          Figure 1.3: Tables used for converting between binary, octal, and hexadecimal.


                  digit. Conversely, each hexadecimal digit can be converted to exactly four binary digits, and
                  each octal digit can be converted to exactly three binary digits. This relationship makes it pos-
                  sible to do very fast conversions using the tables shown in Fig. 1.3.

                  When converting from hexadecimal to binary, all that is necessary is to replace each hex digit
                  with the corresponding binary digits from the table. For example, to convert 5AC4 16 to bi-
                  nary, we just replace “5” with “0101,” replace “A” with “1010,” replace “C” with “1100,”
                  and replace “4” with “0100.” So, just by referring to the table, we can immediately see that
                  5AC4 16 = 0101101011000100 2 . This method works exactly the same for converting from
                  octal to binary, except that it uses the table on the right side of Fig. 1.3.

                  Converting from binary to hexadecimal is also very easy using the table. Given a binary
                  number, n, take the four least significant digits of n and find them in the table on the left
                  side of Fig. 1.3. The hexadecimal digit on the matching line of the table is the least signif-
                  icant hex digit. Repeat the process with the next set of four bits and continue until there
                  are no bits remaining in the binary number. For example, to convert 0011100101010111 2
                  to hexadecimal, just divide the number into groups of four bits, starting on the right, to
                  get: 0011|1001|0101|0111 2 . Now replace each group of four bits by looking up the cor-
                  responding hex digit in the table on the left side of Fig. 1.3, to convert the binary num-
                  ber to 3957 16 . In the case where the binary number does not have enough bits, simply
                  pad with zeros in the high-order bits. For example, dividing the number 1001100010011 2
                  into groups of four yields 1|0011|0001|0011 2 and padding with zeros in the high-order
   24   25   26   27   28   29   30   31   32   33   34