Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [64]

By Root 1579 0
that begin with the hexa- prefix (such as hexagon or hexapod or hexameter) refer to six of something. Hexadecimal is supposed to mean sixteen. And even though The Microsoft Manual of Style for Technical Publications clearly states, "Do not abbreviate as hex, " everyone always does and I will too.

That's not the only peculiarity of hexadecimal. In decimal, we count like this:

0 1 2 3 4 5 6 7 8 9 10 11 12…

In octal, you'll recall, we no longer need digits 8 and 9:

0 1 2 3 4 5 6 7 10 11 12…

Similarly, the base-4 number system also doesn't need 4, 5, 6, or 7:

0 1 2 3 10 11 12…

And binary, of course, needs only 0 and 1:

0 1 10 11 100…

But hexadecimal is different because it requires more digits than decimal. Counting in hexadecimal goes something like this:

0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ? 10 11 12…

where 10 (pronounced one-zero) is actually 16TEN. The question marks indicate that we need six more symbols to display hexadecimal numbers. What are these symbols? Where do they come from? Well, they weren't handed down to us in tradition like the rest of our number symbols, so the rational thing to do is make up six new symbols, for example:

Unlike the symbols used for most of our numbers, these have the benefit of being easy to remember and identify with the actual quantities they represent. There's a 10-gallon cowboy hat, a football (11 players on a team), a dozen donuts, a black cat (associated with unlucky 13), a full moon that occurs about a fortnight (14 days) after the new moon, and a knife that reminds us of the assassination of Julius Caesar on the ides (the 15th day) of March.

Each byte can be expressed as two hexadecimal digits. In other words, a hexadecimal digit is equivalent to 4 bits, or 1 nibble. The table on the next page shows how to convert between binary, hexadecimal, and decimal.

Binary

Hexadecimal

Decimal

Binary

Hexadecimal

Decimal

0000

0

0

1000

8

8

0001

1

1

1001

9

9

0010

2

2

1010

10

0011

3

3

1011

11

0100

4

4

1100

12

0101

5

5

1101

13

0110

6

6

1110

14

0111

7

7

1111

15

Here's how to represent the byte 10110110 in hexadecimal:

And it doesn't matter if we're dealing with multibyte numbers:

One byte is always represented by a pair of hexadecimal digits.

Unfortunately (or perhaps, much to your relief), we really aren't going to be using footballs and donuts to write hexadecimal numbers. It could have been done that way, but it wasn't. Instead, the hexadecimal system ensures that everybody gets really confused and stays that way. Those six missing hexadecimal digits are actually represented by the first six letters of the Latin alphabet, like this:

0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12…

The following table shows the real conversion between binary, hexadecimal, and decimal:

Binary

Hexadecimal

Decimal

0000

0

0

0001

1

1

0010

2

2

0011

3

3

0100

4

4

0101

5

5

0110

6

6

0111

7

7

1000

8

8

1001

9

9

1010

A

10

1011

B

11

1100

C

12

1101

D

13

1110

E

14

1111

F

15

The byte 10110110 can thus be represented by the hexadecimal number B6 without your drawing a football. As you'll recall from previous chapters, I've been indicating number bases by subscripts, such as

10110110TWO

for binary, and

2312FOUR

for quaternary, and

266EIGHT

for octal, and

182TEN

for decimal. To continue the same system, we can use

B6SIXTEEN

for hexadecimal. But that's clearly excessive. Fortunately, several other, terser, methods of denoting hexadecimal numbers are common. You can indicate the numbers this way:

B6HEX

In this book, I'll be using mostly a very common approach, which is a lowercase h following the number, like so:

B6h

In a hexadecimal number, the positions of each digit correspond to powers of 16:

The hexadecimal number 9A48Ch is

9A48Ch = 9 x 10000h +

A x 1000h +

4 x 100h +

8 x 10h +

C x 1h

This can be written using powers of 16:

9A48Ch = 9 x 164 +

A x 163 +

4 x 162 +

8 x 161 +

C x 160

Or using the decimal equivalents of those powers:

Return Main Page Previous Page Next Page

®Online Book Reader