Wednesday, 24 October 2012

Number Bases

Before we go any further, we need to talk about number bases. It's so fundamental to the way microcontrollers work that not much more can be done until we fully understand at least 2 different number bases, binary and hexadecimal.

If someone tells you that their number of friends is in single figures (or single digits) we immediately know the number is between zero and 9. That's because we (human beings) do all our counting in base 10, and in base 10, the number of different numerical characters is 10. But note that there is no single digit representation of the number 'ten' in base 10. Ten has to be expressed using two digits, one and zero, specifically a 'one' followed by a 'zero'.

Similarly, in base 2, there is no single digit representation of the number 'two'.

Now look at the 16F690 data sheet, figure 2-5 SPECIAL FUNCTION REGISTERS and you'll notice that after register 09h is register 0Ah (PCLATH). It's not until a bit further down that you'll find register 10h. But 10h is not 'ten' as we would normally understand it. In fact 10h is sixteen.

The clue here is the little h after the number. Actually it's more than just a clue, it's a clear indication that this number is not expressed in base 10. It's expressed in base sixteen, also called hexadecimal.

In PIC programming, the number base is also called the Radix and in assembly language there's a default radix. That's the number base that's assumed if your numerals have no radix indicator like the lowercase h that indicates hexadecimal. The default radix is hexadecimal, so in the third line of our world's smallest program, 87 is not 87 decimal but 87 hex (87h).

I've always felt that using the letters A to F to represent the six additional digits in hexadecimal is a bit of a cop out. I'd like to have seen six new number characters drawn up - six new exotic squiggles that would have be learnt. But that's not what happened and so A to F is what we have to work with.

Now it's my time to cop out and refer you to the Wikipedia article on Radix. It's very very short and so easily digested in a couple of minutes.

No comments:

Post a Comment