Appendix A. Tutorial: Working with Binary and Hex

The best way to gain an understanding of binary and hexadecimal numbering is to begin by examining the decimal numbering system. The decimal system is a base 10 numbering system. (The root deci means ten.) Base 10 means that there are 10 digits with which to represent numbers: 0 through 9. Most likely, we work in base 10 because our ancient ancestors began counting their cattle and children—and enemies—on their fingers (in fact, the word digit means finger).

The use of place values allows the representation of large numbers with a few digits, such as the 10 decimal digits. The place values of all numbering systems begin at the right, with the base raised to the power of 0. Reading to the left, each place value is the base raised to a power that is one more than the power of the previous place value:

    B4 B3 B2 B1 B0

In base 10, the first five place values are

    104 103 102 101 100

The first two place values are easy to calculate for any base. Any number raised to the power of 0 is 1; so 100 = 1. Any number raised to the power of 1 is simply that number; so 101 = 10. The third place value is easy to calculate. Simply multiply the second place value by the base. In fact, each place value can be calculated by multiplying the previous place value by the base. So, given the five place values above,

    100 = 1    101 = 1 × 10 = 10    102 = 10 × 10 = 100    103 = 100 × 10 = 1000    104 = 1000 × 10 = 10,000

So, the first five place values of the base 10 numbering system are

    10,000  1000  100  10  1

Reading a number such as 57,258 in terms of place values means there are five quantities of 10,000, seven quantities of 1000, two quantities of 100, five quantities of 10, and eight quantities of 1. That is,

    5 × 10,000 = 50,000    7 × 1000 = 7000    2 × 100 = 200    5 × 10 = 50    8 × 1 = 8

Adding these individual results together, the result is 50,000 + 7000 + 200 + 50 + 8 = 57,258.

All of us are so acquainted with working in base 10 that we seldom think of breaking a number down into its place values. However, this technique is essential to decipher numbers in other bases.

Working with Binary Numbers

Computers are, at the most fundamental level, simply a collection of electrical switches. Numbers and characters are represented by the positions of these switches. Because a switch has only two positions, on or off, it uses a binary, or base 2, numbering system. (The root bi means two.) A base 2 system has only two digits: 0 and 1.

Computers usually group these digits into eight place values, known as a byte or an octet. The eight place values are

    27 26 25 24 23 22 21 20

The place values are calculated as follows:

    20 = 1    21 = 1 × 2 = 2    22 = 2 × 2 = 4    23 = 4 × 2 = 8    24 = 8 × 2 = 16    25 = 16 × 2 = 32    26 = 32 × 2 = 64    27 = 64 × 2 = 128

So, the place values of a binary octet are

    128  64  32  16  8  4  2  1

Thus, the binary octet 10010111 can be read as follows:

    1 × 128 = 128    0 × 64 = 0    0 × 32 = 0    1 × 16 = 16    0 × 8 = 0    1 × 4 = 4    1 × 2 = 2    1 × 1 = 1    or 128 + 16 + 4 + 2 + 1 = 151

Working in binary is easy because for every place value there is either one quantity of that value or none of that value. For another example, 11101001 = 128 + 64 + 32 + 8 + 1 = 233.

Where converting binary to decimal is a matter of adding the place values, converting from decimal to binary is a matter of subtracting place values. To convert the decimal number 178 to binary, for instance, begin by subtracting the highest base 2 place value possible from the number:

  1. 178 is greater than 128, so we know there is a 1 at that place value: 178 – 128 = 50.

  2. 50 is less than 64, so there is a 0 at that place value.

  3. 50 is greater than 32, so there is a 1 at that place value: 50 – 32 = 18.

  4. 18 is greater than 16, so there is a 1 at that place value: 18 – 16 = 2.

  5. 2 is less than 8, so there is a 0 at that place value.

  6. 2 is less than 4, so there is a 0 at that place value.

  7. 2 is equal to 2, so there is a 1 at that place value: 2 – 2 = 0.

  8. 0 is less than 1, so there is a 0 at that place value.

Putting the results of all these steps together, 178 is 10110010 in binary.

Another example might be helpful. Given 110,

  1. 110 is less than 128, so there is a 0 at that place value.

  2. 110 is greater than 64, so there is a 1 at that place value: 110 – 64 = 46.

  3. 46 is greater than 32, so there is a 1 at that place value: 46 – 32 = 14.

  4. 14 is less than 16, so there is a 0 at that place value.

  5. 14 is greater than 8, so there is a 1 at that place value: 14 – 8 = 6.

  6. 6 is greater than 4, so there is a 1 at that place value: 6 – 4 = 2.

  7. There is a 1 at the 2 place value: 2 – 2 = 0.

  8. 0 is less than 1, so there is a 0 at that place value.

Therefore, 110 is 01101110 in binary.

Working with Hexadecimal Numbers

Writing out binary octets isn’t much fun. For people who must work with such numbers frequently, a briefer notation is welcome. One possible notation is to have a single character for every possible octet; however, there are 28 = 256 different combinations of eight bits, so a single-character representation of all octets would require 256 digits, or a base 256 numbering system.

Life is much easier if an octet is viewed as two groups of four bits. For instance, 11010011 can be viewed as 1101 and 0011. There are 24 = 16 possible combinations of four bits, so with a base 16, or hexadecimal, numbering system, an octet can be represented with two digits. (The root hex means six, and deci means ten.) Table A-1 shows the hexadecimal digits and their decimal and binary equivalents.

Table A-1. Hex, decimal, and binary equivalents.

Hex

Decimal

Binary

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

9

9

1001

A

10

1010

B

11

1011

C

12

1100

D

13

1101

E

14

1110

F

15

1111

Because the first 10 characters of the decimal and the hexadecimal numbering system are the same, it is customary to precede a hex number with a 0x, or follow it with an h, to distinguish it from a decimal number. For example, the hex number 25 would be written as 0x25 or as 25h. This book uses the 0x convention.

After working with binary for only a short while, it is easy to determine a decimal equivalent of a 4-bit binary number in your head. It is also easy to convert a decimal digit to a hex digit in your head. Therefore, converting a binary octet to hex is easily done in three steps:

  1. Divide the octet into two 4-bit binary numbers.

  2. Convert each 4-bit number to decimal.

  3. Write each decimal number in its hex equivalent.

For example, to convert 11010011 to hex,

  1. 11010011 becomes 1101 and 0011.

  2. 1101 = 8 + 4 + 1 = 13, and 0011 = 2 + 1 = 3.

  3. 13 = 0xD, and 3 = 0x3.

Therefore, 11010011 in hex is 0xD3.

Converting from hex to binary is a simple matter of working the three steps backward. For example, to convert 0x7B to binary,

  1. 0 x 7 = 7, and 0xB = 11.

  2. 7 = 0111, and 11 = 1011.

  3. Putting the 4-bit numbers together, 0x7B = 01111011, which is decimal 123.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset