# Number System Primer

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 |
---|---|---|

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 |
---|---|---|---|---|

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.

256 | 16 | 1 |
---|---|---|

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 |
---|---|---|

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!

The hex table should read 256 instead of 32. ;)

Maarten: fixed, thanks! One day I'll proofread *before* I press publish :)

I was going to say the same. looks like you're corrected it to 128 instead of 256, though.

easy way to figure it out: each new column is the previous multiplied by the base. 1*16=16, 16*16=256

Fixed properly this time. Thanks for pointing it out ... I have no excuse other than I was at a conference and clearly not concentrating :(

Great post, very helpful. Glad it's not just me that has found the only place to use some of these is in answering ZCE questions.

The only place I've seen the octal system being used is in file-permissions on *nix-Systems - and therefore in PHP's file- and folder-handling functions

I'm shocked to read about this! What's the problem with the education systems in western countries that people with a technical background don't know about other numbering systems than decimal.

It's taught in secondary school!

And if not taught at the secondary school, nobody of you wondered what all this HEX, BIN, DEC etc. mean in your father's pocket calculator ;)

When I started working in computing in the early eighties you needed to know your hexadecimal or octal. I worked for Honeywell who used hexadecimal, but IBM and I think ICL used Octal.

I think octal is important mainly so people realise why

[geshi lang=php]

$var = 09;

[/geshi]

is an error (because octal is denoted by leading 0s, but 9 isn't a valid octal digit). This significantly confused me when autogenerating an include file with dates in it - everything worked except for some days near the start of each month ...

Chris: Yeah, I find it interesting academically, but I really don't use it.

Andreas: Ah, that's a very good point, I hadn't thought of those. Thanks :)

Meketrefe: I definitely did binary at school, and hex at university - but most web developers aren't using these skills and have forgotten the detail of how it all works over time. My hope is that this post will serve as a quick refresher!

Hilary: I did not know that, thanks for the interesting information.

Richard: That's also a valid use case; it's niche but as you say, quite easy to stumble across it

Exactly why octal is included is a mystery to me, I've never used it other than to answer ZCE exam questions.

Are you sure?!! You never used chmod??? ;)

schkovich: Well if I'm going to change file permissions, it's usually a one-off event and I do that from command-line with the character arguments to chmod. In PHP, most things are owned by the web server and that's fine - so I guess I don't really use chmod with the numbers.

Octal is also used for the umask: http://in2.php.net/umask

and as an easy way to enter non-printable characters into strings:

"\000" is a string containing the null byte.

Here is a good resource for people wanting to learn binary and hexadecimal.

http://justwebware.com/bitwise/bitwise.html

Just curious if this topic is also in the new Zend exams ..I mean 5.5? Thanks

Yes, this topic is still in ZCE for 5.5 (they didn't change the syllabus significantly from 5.3)