PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Number System Primer

Note: This article was originally published at Planet PHP on 27 January 2011.
Planet PHP
I regularly teach the Zend Certification course and one thing that really splits those with a Computer Science background from those without is handling number systems other than decimal. Personally I don't see it as a major omission, I certainly don't work with those systems very often in web development! However, ZCE includes decimal, binary, octal and hexadecimal numbers in its syllabus, so I always make sure to stop and teach it. Here's a quick round-up of how they work:

Decimal


We all know how to count in decimal, but there are a few things about it that I'd like us to notice before we move on. When we learn to count and particularly to add up numbers, we are taught to make three columns, hundreds, tens and units. In the units column can appear any digit from 0-9. When the number is bigger than that, we put a number in the tens column, and so on.


100
10
1


A
A
0-9




Binary


Binary can have only two possible digits in each column, 0 or 1. So our units column can be zero or one, but then next column along is units of 2 instead of units of 10. So 2 in decimal becomes 10 in binary, 3 decimal is 11 binary, and 4 decimal is 100 binary ... hopefully by now you see the pattern! The columns go up in powers of 2 instead of powers of 10.


16
8
4
2
1


A
A
A
A
0-1


Binary is interesting because it's a series of flags - I'll write another time about bits and shifting and logical operations.

Hexadecimal


Again, this is all the same idea as before, everything keeps counting but we use a different number of symbols in the units column, and so the next column up has a bigger value. With hex, we can count to 16 rather than 10 - so we use digits 0-9 and then a, b, c, d, e and f to make up to 16. We work with this number system all the time since html colours are hexadecimal numbers, so I often think this is the most familiar other system for web developers.


32
16
1


A
A
0-f



It isn't unusual to see hex used in calculations, and the trick with this is to write down the columns and concentrate hard. So for example a3 in hex is 163 - because the digit a represents 10, and is in the 16 column, so we have 160, plus the 3 from the units column.

Octal


Exactly why octal is included is a mystery to me, I've never used it other than to answer ZCE exam questions. Perhaps there's a useful application that I'm not aware of, in which case I'm interested to hear what that is! In any event, you have the idea by now and octal works in exactly the same way: we can have digits 0-7 in the units column, and the other columns go up in powers of 8:


64
8
1


A
A
0-7



Number Systems


Number systems are not complicated although depending on your background they may be unfamiliar. My recommendation is to set yourself some simple decimal arithmetic questions (links to examples are appreciated) and then try them in each of the other number systems, to practice working with the different sets of digits. This was a simple explanation but it isn't rocket science at all, and I hope it was helpful!