Hexadecimal |
Written by Harry Fairhead | ||||||||||
Friday, 15 January 2021 | ||||||||||
Page 1 of 3 Hexadecimal is the most common way of displaying the raw data sitting in a machine's memory, but if you are not familiar with it you might ask "What the hex..?" What Programmers Know
Contents
* Recently revised What the hex?Hexadecimal is the most common way of displaying the raw data sitting in a machine's memory or even stored on disk. You can be happily programming away in a high level language without a care in the world and then suddenly an serious error occurs and you are faced with a line showing you the address of the problem and the contents of the processors registers etc. all in glorious hex. Even if you are not programming, the most usual format to dump a file in is as lines and lines of hex. Back in the dark old days of assembly language programming you had to be familiar with hex as well as with binary and occasionally octal. If you haven't mucked about with assembler or machine architecture, or if you fell asleep in the first term of the computer course, then you might think hex is just a programmer's curse. To make sure that you know better this is a short, and highly practical guide, to hexadecimal addressing and data. If you are no good at math don't panic because it is very simple and after a few minutes practice it becomes almost second nature. Sixteen fingersIf only we had all been born with 16 fingers! Not only would typing have been a faster activity but we might have counted in hex naturally. Counting is a matter of using symbols to represent each number. For example, 0, 1, 2, 3 and so on. Using this simple system the problem is that you quickly run out of symbols. You need a symbol for each possible quantity of things. The solution is to use a place value counting method. In decimal we are all very happy with this method - you count up to 9 and then start again after recording the fact that you have got to 10 once by writing a 1 to the left. In school we are taught that each place to the left represents 10 times more than the previous digit location. That is, the first value represents units, the second lots of 10, the third lots of 100 and so on. This means that a number like 123 is really:
or one lot of 100, two lots of 10 and three lots of 1. It is generally supposed, and I can think of no better explanation, that we count in lots of 10 because that's how many fingers we have.
All of this is so easy that we tend to use it intuitively and without being able to explain what it going on. This makes the shock of changing to a different counting base even more traumatic. Hexadecimal uses 16 as the base - Hexa=6 and decimal=10. To put this another way the hex counting system uses `lots of 16' in the same way that the decimal system uses `lots of 10'. The first problem to be solved is how to count up to the first lot of 16 as there are only ten digits - 0 to 9. The solution is that we use the letters A, B, C, D, E and F to supplement the meager inheritance that having only ten fingers has bestowed upon us.
This means that counting in hex up to 15 goes 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. The only tricky bit being to remember not to say `ten' after 9. Now what happens after counting to F? This is, the same question as what happens after counting to 9 in decimal. The answer is that you write a 1 to the left to indicate that you have counted one lot of 16 and then carry on counting. That is after F comes 10 which isn't ten (decimal) and shouldn't really be said as ten but `one nought' or `one zero'. The next question is what comes after 10 in hex? The answer is 11, then 12 and so on all the way up to 1F. At this point we have another lot of 16 and we start counting again at 20 and so on. The only danger point happens when you reach 9F and the temptation is to accidentally make the next value 100. It isn't because in hex A comes after 9 and so the next value is A0 and so on. You only reach 100 in hex after counting all of the way to FF. Fun isn't it? Once you have learned to count to 100 hex there isn't anything more to see. You just keep counting up to F and adding one to the place to the left until it reaches F and so on. Many programming languages use the convention, first used in C, that a hex number is indicated by starting with 0x. So if you have counted to FF you can write this as 0xFF. Remember: the hex place value system works with lots of 16 and not lots of 10. <ASIN:0750675438> <ASIN:3540633693> <ASIN:1553957768> |
||||||||||
Last Updated ( Friday, 15 January 2021 ) |