Learning to Count Again – As a Programmer

Posting once a month on a blog is not a big commitment, but somehow I managed to not post at all in February and have only just scraped in for March. If anyone other than myself were holding me to account, my excuses would include that I went to Ruby Conf, I went to Japan for 2 and a half weeks, and that I have been busy organising the next Sydney Rails Girls. I would also say that I have not been idle programming and learning-wise. In fact, the point of this blog was to be a record of things I learnt – so I wanted to note that I read Steven Frank’s book “How to Count”. I read this book because at the recent Ruby Conf it was recommended to me during a Go Programming workshop run by Katrina Owen.

I was having trouble coming up with a solution to a problem, and it turns out that one of the possible solutions involved using a whole lot of ‘powers of 2′ calculations. It was something to do with binary – and as I had never done computer science, I had no idea about anything to do with binary. Katrina Owen recommended Steven Franks book – and actually it was perfectly pitched for someone like me.

Titled ‘How to Count’ it takes you through 3 ways to count.

Counting in Decimal

First it starts with decimal explaining how you actually count in decimal. That sounds simple – and it was – but it sets you up well for the next chapters.

Counting in decimal means counting with base 10. Base 10 means you have 10 characters to choose from 0 – 9.

When you could you start at 0 and count up to 9, and when you run out of characters, what do you do? We do it so naturally that it was actually good to have this book point out the pattern of what we do. We click the 9 back over to 0 to start again, and we add 1 to the left. We do it every time we get to 9. And that’s how we can use just 10 characters to count up and up!

Counting in Binary

Then he takes us into Binary, which I loved. B

ecause binary is a word that is bandied about, and  I never understood what it meant.

Binary means Base 2. That means you have just 2 characters to count with  0 and 1. To count you still iterate through, you start at 0, then get to 1, then…what to do ? Same as with decimal, click back round to 0, and add 1 to the left.









This way you can also count up and up with only 2 numbers. And why do we always hear the word ‘binary’ being bandied about in relation to computer science? Because at the very basis, computers run on the idea of sending electricity along a wire. You can send a message by noting whether there is electricity there or not. Just arbitrarily, perhaps the absence of an electronic signal on that wire means 0, and the presence of a signal means 1. So with one wire and electricity, you can count to 1!

Then, say if you had 2 wires. You could count to a maximum of three. The first wire could be 0 or 1, depending if it was on or off, then you can combine that with two possible options on the second wire. And so on.

There is a handy way of converting between decimal and binary.  To go from binary to decimal, you can use a table like this. Align each 1 and 0 with a power of 2 that it represents, then add up those values.

Binary to decimal conversion




How to convert from decimal to binary? Well, say you have a number like 53, the biggest power of 2 that comes out of that is 32, which is 2 to the power of 5. So using a table similar to the one above, you put a 1 in the 32 box.

Then you have 21 left.  The next biggest power of 2 to come out of that is 16, so you put a 1 in the 16 box.

Then you have 5 left, out of which comes 2 to the power of 2 (4). So put that in the 4 box. Then you have one left over to go in the 1 box. So your binary representation of 53 is 110101.

Cheats way to convert from binary to decimal (and this is probably such a random way, there are probably much easier ways to do this). Javascript has a  ParseInt function which changes strings into integers. The second parameter to this function can be a base, so if you want to change a binary number into an integer you can do something like this

parseInt("11110100", 2) # which will be 244

So whenever you’re in a browser, you can just open up your console and just use Parseint to make binary into decimal integers.

Counting in Hexadecimal

The last way of counting we learnt was Hexadecimal, or hex. This counts to 16. It goes 0-9 and then A-F. And this way you can count all the way up to 16 with just 1 character, whereas with decimal you’d need at least 2.

He didn’t go too far into hexadecimal, just enough to show how to count in it, but not a lot on conversion. You could use the Javascript method I showed above to convert hexadecimal numbers into decimal though, just open up your browser console and go
parseInt("A", 16) # which is 10

Why care about floating point versus integers?

This is another topic I didn’t know about. Why care about if a number is floating point or integer? Which would result in me randomly using either. Well, Frank’s book explains why it matters, and I discovered his point is also succinctly covered in this ‘Dummies’ article.

The bottom line is that it takes more work for the computer to figure out binary problems, like floating-point values, than it does for the computer to work in integers. So, wherever possible, use integer values; use the floating-point numbers only when necessary.

Steven Frank touted this as the first in a series of ‘Programming for mere mortals’ books. I’m a mere mortal, I loved this book – but it was published in 2011, and there hasn’t been another since, so that’s a bit of a shame.

Leave a reply