Prev:
Quiz and Exercises on Program Flow
Next:
Binary Numbers
This section is probably past the basic level, but it pays to have some understanding of what hexadecimal is.
From an early age, we've been taught how to count using the DECIMAL system, where the numbers consist of a mixture of the ten digits from 0 to 9.
We say the the decimal system has a BASE of 10 - the number of digits we can use to form our numbers.
What do you think when you see the number "237"?
"Two hundred and thirty seven"?
To understand different bases, try to think of it as "2 lots of 102, 3 lots of 101 and 7 lots of 100", or:
2*100 + 3*10 + 7*1 = 237.
So what's "4560"?
"4 lots of 103, 5 lots of 102, 6 lots of 101 and 0 lots of 100", or:
4*1000 + 5*100 + 6*10 + 0*1 = 4560
This section can get quite confusing, but I'll do my very best! But in a nutshell...
HEXADECIMAL IS BASE 16.
We still use digits 0 to 9, but the letters A, B, C, D, E, and F are used to represent the decimal numbers 10, 11, 12, 13, 14 and 15 respectively.
But if I write "38", do I mean 38 in decimal or 38 in hexadecimal? Who knows...
3810 is the formal notation for saying "Three eight base ten" (or "Three eight in decimal"), where as 3816 represents "Three eight in hexadecimal".
- The subscript number is the base.
Having said that, it'd be easier if I stuck to words instead!
So what does "38" in hexadecimal really mean? In decimal: "3 lots of 161 and 8 lots of 160", or:
3*16 + 8*1 = 48 + 8 = 56.
What decimal number does "21C" in hexadecimal represent? "2 lots of 162, 1 lot of 161 and 12 lots of 160", or:
2*256 + 1*16 + 12*1 = 512 + 16 + 12 = 540.
The past two examples saw the relatively easy conversion from hexadecimal back to decimal.
Before I move on, the case of the hexadecimal digits A to F do not matter - uppercase does seem more readable though, which is why I use it.
But first, let's go through some powers of 16...
|
160 = 1 |
= 1 |
|
|
161 = 16 |
= 10 |
|
|
162 = 256 |
in hex... |
= 100 |
|
163 = 4096 |
= 1000 |
|
|
164 = 65536 |
= 10000 |
Now take a decimal number like 8875.
Find the highest power of 16 that is smaller or equal to 8875: 163 in this case.
The number of the highest power plus 1 tells us how long the resulting hexadecimal number is going to be, so in this case, 3+1 = 4 hexadecimal digits.
Now work out how many times 163 goes into 8875:
8875 / 4096 = 2.1667.... so 4096 goes into 8875 2 times and so the leftmost hexadecimal digit is 2.
Now work out the remainder of 8875 / 163:
Remainder = 8875 - (2*4096) = 683.
Alternatively:
Remainder = 8875 % 4096.
The % operator was covered a while ago in the Arithmetic Operators section.
Now, decreasing the power, find out how many times 162 goes into 683:
683 / 256 = 2.6679.... so 256 goes into 683 2 times. Therefore the next hexadecimal digit is 2.
Now work out the remainder of 683 / 162:
Remainder = 683 - (2*256) = 171.
Now find out how many times 161 goes into 171:
171 / 16 = 10.6875 so 16 goes into 171 10 times. Therefore the next hexad ecimal digit is A (for 10).
Now work out the remainder of 171 / 161:
Remainder = 171 - (10*16) = 11.
Now we're below the base value, so the rightmost hexadecimal digit is B (for 11).
Stringing the hexadecimal digits together, we get 22AB as the hexadecimal equivalent of 887510.
Quick check:
2*4096 = 8192
2*256 = 512
10*16 = 160
11*1 = 11
8192 + 512 + 160 + 11 = 8875
Note that precision is lost when you begin entering huge numbers!
Make sure you check to see if the number converts back to the original.
Hexadecimal is sometimes abbreviated with HEX.
Expressing negative decimal numbers in hex format is possible, but as a beginner, you probably won't need to know how.
There is a hex format specifier: %X for an uppercase representation, or %x for lowercase.
To initialize a hex number in C, prefix the hex number with 0x (zero x), like this:
int i = 0xFF; /* assigns 255 to i */
I will briefly mention OCTAL numbers. They have a base of 8, and work in a similar way to hex, for example: 65 in decimal is 101 in octal because: 1*82 + 0*81 + 1*80 = 65.
The octal format specifier is %o - you probably won't use octal numbers as often as hex though (if ever!).
#include <stdio.h>
int main() {
int i = 0xF0;
printf("%d is %X in hex\n", i, i);
printf("%d is %o in octal\n", i, i);
printf("%d is %X in hex\n", -i, -i);
printf("%d is %o in octal\n", -i, -i);
return 0;
}
Output:
240 is F0 in hex |
Negative hex is mentioned in the bitwise operators section, but you probably don't need to know it!
For those of you who are learning HTML to build web pages, color codes in HTML are expressed in hexadecimal format, so this lesson would've helped you understand it a little!
A color code is of the form: #RRGGBB where R, G, B are the hex digits for red, green and blue. The minimum value is 00 (zero!), and maximum is FF (255). Red is therefore #FF0000 (maximum red, zero green and blue). Black is #000000.
Prev:
Quiz and Exercises on Program Flow
Next:
Binary Numbers
www.iota-six.co.uk
Copyright © 2001-2003
Unauthorized copying not permitted
Designed and Developed Using Macromedia Studio MX