Appendix A. Working with Numbers: Binary and Hexadecimal

You learned the fundamentals of arithmetic so long ago, it is hard to imagine what it would be like without that knowledge. When you look at the number 145, you instantly see “one hundred forty-five” without much reflection.

You generally see numbers in what is called the decimal format. There are, however, other formats that can be used for numbering. When working with computers, the two systems that come up the most are binary and hexadecimal. Understanding binary and hexadecimal requires that you reexamine the number 145 and see it not as a number, but as a code for a number.

Start small: Examine the relationship between the number three and “3.” The numeral “3” is a squiggle on a piece of paper; the number three is an idea. The numeral is used to represent the number.

The distinction can be made clear by realizing that three, 3, |||, III, and *** all can be used to represent the same idea of three.

In base 10 (decimal) math, you use ten symbols—the numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9—to represent all numbers. How is the number ten represented?

You can imagine that a strategy could have evolved of using the letter A to represent ten; or IIIIIIIIII could have been used to represent that idea. The Romans used X. The Arabic system, which we use, makes use of position in conjunction with numerals to represent values. The first (rightmost) column is used for ones, and the next column (to the left) is used for tens. Thus, the number fifteen is represented as 15 (read “one, five”); that is, 1 ten and 5 ones.

Certain rules emerge, from which some generalizations can be made:

1. Base 10 uses ten digits—the digits 0–9.

2. The columns are powers of ten: 1s, 10s, 100s, and so on.

3. If the third column is 100, the largest number you can make with two columns is 99. More generally, with n columns you can represent from 0 to (10n-1). Thus, with three columns, you can represent from 0 to (103-1) or 0-999.

Using Other Bases

It is not a coincidence that we use base 10; we have 10 fingers. You can imagine a different base, however. Using the rules found in base 10, you can describe base 8:

1. There are eight digits used in base 8—the digits 0–7.

2. The columns are powers of 8: 1s, 8s, 64s, and so on.

3. With n columns, you can represent 0 to 8n-1.

To distinguish numbers written in each base, write the base as a subscript next to the number. The number fifteen in base 10 would be written as 1510 and read as “one, five, base ten.”

Thus, to represent the number 1510 in base 8, you would write 178. This is read “one, seven, base eight.” Note that it can also be read “fifteen” as that is the number it continues to represent.

Why 17? The 1 means 1 eight, and the 7 means 7 ones. One eight plus seven ones equals fifteen. Consider fifteen asterisks:


**********     *****

The natural tendency is to make two groups, a group of ten asterisks and another of five. This would be represented in decimal as 15 (1 ten and 5 ones). You can also group the asterisks as


********          *******

That is, eight asterisks and seven. That would be represented in base 8 as 178. That is, one eight and seven ones.

Converting to Different Bases

You can represent the number fifteen in base 10 as 15, in base 9 as 169, in base 8 as 178, in base 7 as 217. Why 217? In base 7, there is no numeral 8. To represent fifteen, you need two sevens and one 1.

How do you generalize the process? To convert a base 10 number to base 7, think about the columns: In base 7 they are ones, sevens, forty-nines, three-hundred forty-threes, and so on. Why these columns? They represent 70, 71, 72, 74, and so forth.

Remember, any number to the 0th power (for example, 70) is 1, any number to the first power (for example, 71) is the number itself, any number to the second power is that number times itself (72 = 7*7 = 49), and any number to the third power is that number times itself and then times itself again (73 = 7*7*7 = 343).

Create a table for yourself:

Image

The first row represents the column number. The second row represents the power of 7. The third row represents the decimal value of each number in that row.

To convert from a decimal value to base 7, here is the procedure: Examine the number and decide which column to use first. If the number is 200, for example, you know that column 4 (343) is 0, and you don’t have to worry about it.

To find out how many 49s there are, divide 200 by 49. The answer is 4, so put 4 in column 3 and examine the remainder: 4. There are no 7s in 4, so put a zero in the 7s column. There are 4 ones in 4, so put a 4 in the 1s column. The answer is 4047.

Image

In this example, the 4 in the third column represents the decimal value 196, and the 4 in the first column represents the value 4. 196+4 = 200. Thus, 4047 = 20010.

Try another example. Convert the number 968 to base 6:

Image

Be certain you are comfortable with why these are the column values. Remember that 63 = 6*6*6 = 216.

To determine the base 6 representation of 968, you start at column 5. How many 1296s are there in 968? There are none, so column 5 has 0. Dividing 968 by 216 yields 4 with a remainder of 104. Column 4 is 4. That is, column 4 represents 4*216 (864).

You must now represent the remaining value (968-864 = 104). Dividing 104 by 36 yields 2 with a remainder of 32. Column 3 is 2. Dividing 32 by 6 yields 5 with a remainder of 2. The answer therefore is 42526.

Image

864+72+30+2 = 968.

Binary

Base 2 is the ultimate extension of this idea. In base 2, also called binary, there are only two digits: 0 and 1. The columns are

Image

To convert the number 88 to base 2, you follow the same procedure: There are no 128s, so column 8 is 0.

There is one 64 in 88, so column 7 is 1 and 24 is the remainder. There are no 32s in 24 so column 6 is 0.

There is one 16 in 24 so column 5 is 1. The remainder is 8. There is one 8 in 8, and so column 4 is 1. There is no remainder, so the rest of the columns are 0.

Image

To test this answer, convert it back:


1 * 64  =  64
0 * 32  =   0
1 * 16  =  16
1 *  8   =   8
0 *  4   =   0
0 *  2   =   0
0 *  1   =   0
                 88

Why Base 2?

Base 2 is important in programming because it corresponds so cleanly to what a computer needs to represent. Computers do not really know anything at all about letters, numerals, instructions, or programs. At their core they are just circuitry, and at a given juncture there either is a lot of power or there is very little.

To keep the logic clean, engineers do not treat this as a relative scale (a little power, some power, more power, lots of power, tons of power), but rather as a binary scale (“enough power” or “not enough power”). Rather than saying “enough” or “not enough,” they simplify it to “yes” or “no.” Yes or no, or true or false, can be represented as 1 or 0. By convention, 1 means true or Yes, but that is just a convention; it could just as easily have meant false or no.

After you make this great leap of intuition, the power of binary becomes clear: With 1s and 0s, you can represent the fundamental truth of every circuit (there is power or there isn’t). All a computer ever knows is, “Is you is, or is you ain’t?” Is you is = 1; is you ain’t = 0.

Bits, Bytes, and Nybbles

After the decision is made to represent truth and falsehood with 1s and 0s, binary digits (or bits) become very important. Because early computers could send eight bits at a time, it was natural to start writing code using 8-bit numbers—called bytes.

Note

Half a byte (4 bits) is called a nybble!

With eight binary digits, you can represent up to 256 different values. Why? Examine the columns: If all 8 bits are set (1), the value is 255. (128+64+32+16+8+4+2+1) If none is set (all the bits are clear or zero), the value is 0. 0–255 is 256 possible states.

What’s a KB?

It turns out that 210 (1,024) is roughly equal to 103 (1,000). This coincidence was too good to miss, so computer scientists started referring to 210 bytes as 1K or 1 kilobyte, based on the scientific prefix of kilo for thousand.

Similarly, 1024*1024 (1,048,576) is close enough to one million to receive the designation 1MB or 1 megabyte, and 1,024 megabytes is called 1 gigabyte (giga implies thousand-million or billion). Finally, 1,024 gigabytes is called a terabyte.

Binary Numbers

Computers use patterns of 1s and 0s to encode everything they do. Machine instructions are encoded as a series of 1s and 0s and interpreted by the fundamental circuitry. Arbitrary sets of 1s and 0s can be translated back into numbers by computer scientists, but it would be a mistake to think that these numbers have intrinsic meaning.

For example, the Intel 8086 chipset interprets the bit pattern 1001 0101 as an instruction. You certainly can translate this into decimal (149), but that number per se has no meaning.

Sometimes, the numbers are instructions, sometimes they are values, and sometimes they are codes. One important standardized code set is ASCII. In ASCII, every letter and punctuation is given a seven-digit binary representation. For example, the lowercase letter “a” is represented by 0110 0001. This is not a number, although you can translate it to the number 97 in base 10 (64+32+1). It is in this sense that people say that the letter “a” is represented by 97 in ASCII; but the truth is that the binary representation of 97, 01100001, is the encoding of the letter “a,” and the decimal value 97 is a human convenience.

Hexadecimal

Because binary numbers are difficult to read, a simpler way to represent the same values is sought. Translating from binary to base 10 involves a fair bit of manipulation of numbers; but it turns out that translating from base 2 to base 16 is very simple, because there is a very good shortcut.

To understand this, you must first understand base 16, which is known as hexadecimal. In base 16, there are sixteen numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. The last six are arbitrary; the letters A–F were chosen because they are easy to represent on a keyboard. The columns in hexadecimal are

Image

To translate from hexadecimal to decimal, you can multiply. Thus, the number F8C represents:


F * 256 = 15 * 256 = 3840
8 * 16 =          128
C * 1 = 12 * 1 =      12
3980

(Remember that F in Hexadecimal is equal to 1510.)

Translating the number FC to binary is best done by translating first to base 10, and then to binary:


F * 16 = 15 * 16 =  240
C * 1 = 12 * 1 =     12
252

Converting 25210 to binary requires the chart:

Image

There are no 256s.

1*128 = 128. 252-128 = 124

1*64 = 64. 124-64 = 60

1*32 = 32. 60-32 = 28

1*16 = 16. 28-16 = 12

1*8 = 8. 12-8 = 4

1*4 = 4. 4-4 = 0

0*2 = 0

0*1 = 0

124+60+28+12+4 = 252.

Thus, the answer in binary is 11111100.

Now, it turns out that if you treat this binary number as two sets of four digits (1111 1100), you can do a magical transformation.

The right set is 1100. In decimal that is 12, or in hexadecimal it is C. (1*8 + 1*4 + 0*2 + 0*1)

The left set is 1111, which in base 10 is 15, or in hex is F.

Thus, you have:


1111 1100
F        C

Putting the two hex numbers together is FC, which is the real value of 1111 1100. This shortcut always works! You can take any binary number of any length, and reduce it to sets of four, translate each set of four to hex, and put the hex numbers together to get the result in hex. Here’s a much larger number:


1011 0001 1101 0111

To check this assumption, first convert this number to decimal.

You can find the value of the columns by doubling. The rightmost column is 1, the next is 2, then 4, 8, 16, and so forth.

Start with the rightmost column, which is worth 1 in decimal. You have a 1 there so that column is worth 1. The next column to the left is 2. Again, you have a 1 in that column, so add 2 and for a total of 3.

The next column to the left is worth 4 (you double for each column). Thus, you have 4+2+1 = 7.

Continue this for each column:

Image

Converting this to hexadecimal requires a chart with the hexadecimal values.

Image

The number is less than 65,536, so you can start with the fourth column. There are eleven 4096s (45,056), with a remainder of 471. There is one 256 in 471 with a remainder of 215. There are thirteen 16s (208) in 215 with a remainder of 7. Thus, the hexadecimal number is B1D7.

Checking the math:


B (11) * 4096 =    45,056
1 *      256 =              256
D (13) * 16 =            208
7 * 1 =                           7
Total                    45,527

The shortcut version is to take the original binary number, 1011000111010111, and break it into groups of four: 1011 0001 1101 0111. Each of the four then is evaluated as a hexadecimal number:


1011 =
1 x 1 =    1
1 x 2 =    2
0 x 4 =    0
1 x 8 =    8
Total     11
Hex:       B

0001 =
1 x 1 =    1
0 x 2 =    0
0 x 4 =    0
0 x 8 =    0
Total      1
Hex:       1

1101 =
1 x 1 =    1
0 x 2 =    0
1 x 4 =    4
1 x 8 =    8
Total     13
Hex =      D

0111 =
1 x 1 =    1
1 x 2 =    2
1 x 4 =    4
0 x 8 =    0
Total      7
Hex:       7

Total Hex:  B1D7

Hey! Presto! The shortcut conversion from binary to hexadecimal gives us the same answer as the longer version.

You will find that programmers use hexadecimal fairly frequently in advanced programming; but you’ll also find that you can work quite effectively in programming for a long time without ever using any of this!

Note

One common place to see the use of hexadecimal is when working with color values. This is true in your C++ programs or even in other areas such as HTML.

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

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