geNorNicsTen! Two! Sixteen! Hike!
|
|
Did you ever wonder what it really means when we write '204' down? We all know that '204' is actually two hundreds and a four, but why do we count the way that we do?
Well, technically we count in base 10 or decimal format. In decimal, we count up to nine and then use a zero placeholder to mark a set of ten, then start counting up to nine again, and so on.
Thus, each successive digit in a number represents a further power of ten. Think of it this way:
103 102 101 100 1000 100 10 1
Starting from the right, the last digit represents the number of 100s (100=1) in our number. The digit to the left of that represents the number of 101s (101=10) in the number, etc.
So our '204' really means:
102 101 100 2 0 4 ( 2 x 102 ) + ( 0 x 101 ) + ( 4 x 100 )
We use the '0' to mark the tens place so that we can tell the '2' belongs in the hundreds spot.
Unlike people, however, computers don't have ten fingers and ten toes. They can't count the way that we do, or even think the way that we do. Ultimately, they only know whether a particular electrical circuit is 'on' or 'off'.
With only two choices ( '1' = on, '0' = off ), counting in computers is in base 2 or binary. In this case, each successive digit represents the value of the next power of two:
27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1
Thus a '1' in the third spot from the right would represent a count of '4' in decimal. And our trusty '204' would be written as:
204 = 128 + 64 + 8 + 4
27 26 25 24 23 22 21 20 1 1 0 0 1 1 0 0 ( 1 x 27 ) + ( 1 x 26 ) + ( 0 x 25 ) + ( 0 x 24 ) +
( 1 x 23 ) + ( 1 x 22 ) + ( 0 x 21 ) + ( 0 x 20 )
Computers chug through numbers like '11001100' with no problem. But you can see how reading a long list of such numbers might be a bit harder for us.
Before we go any further, we need to define a few important terms.
On the computer, each 0/1 is called a bit (binary digit), which is the smallest unit of information on the machine. More meaningful information is obtained by combining bits into larger units. For example, a byte (binary term) is a sequence of bits (eight bits in most modern computers) that can be used to represent a single character.
A character is any symbol that we see on a screen or that a computer uses during processing (such as an 'A' or a '7' or an indicator for a line break); each character requires one byte of storage. Different codes of characters exist to represent information. One of the most common is ASCII (American Standard Code for Information Interchange), which represents English characters as numbers, from 0 to 127.
So what's the point? Well, the bottom line is that computer software code uses a lot of eight-bit bytes. We've already shown that reading a whole sequence of binary numbers is rather difficult - we're not as efficient as the computer at visually scanning something like 1011 0100 1101 1110 0101 0011 0111 1001 and understanding what we're seeing.
Enter hexadecimal. Hexa is six and decimal is ten, and thus hexadecimal is base 16 numbering.
Why is that important? Well, consider this. The range in value (in base 10) for an eight-bit byte is zero (0000 0000) to 255 (1111 1111). The same range can be represented in hexadecimal with just two digits. Here's how:
162 161 160 256 16 1
Since we're in base 16, we have to be able to count up to 15 with any one digit before we move to the next exponent and use a zero placeholder. We only have nine digits (1-9) to work with since we are decimal types, so we add the letters A through F.
In other words, in hexadecimal we count '1 2 3 4 5 6 7 8 9 A B C D E F.' Then we mark a set of 161s and use our zero placeholder - so that 10 in hexadecimal represents 16 in decimal. With two digits, we can have up to 15 of the 161s, plus 15 of the 160s. Thu we can count from 0 to 255:
( 15 x 161 ) + ( 15 x 160 ) = 255
This is the same range of numbers covered by an eight-bit byte. Coding in hexadecimal thus saves space (two digits instead of eight) and is a lot easier for us to read.
And that's the end of Lesson 2! Hopefully, you now understand the following terms and concepts:
Some references for further reading: