IT Lecture Notes by Mark Kelly, McKinnon Secondary College

Learning to Count

Binary and Hexadecimal

There is nothing 'natural' about the way we count. The fact that we use the digits from 0 to 9 is a DNA thing - we have evolved with 10 fingers, so a numbering scheme with 10 digits fits in fine.

Cats would probably do it the same way, since they also have 5 digits per foot... except for this one.

If you were paying attention in arithmetic classes in primary school, you'd probably remember that after counting through all 10 digits - 0,1,2,3,4,5,6,7,8,9 you don't need to stop. You can continue infinitely by using another column of digits - 10,11,12,13 etc.

Yep. Amazing. But, strangely, the Romans never thought of that and ended up with huge problems doing basic maths. The concept of indicating numeric size by the position of digits was a damned clever idea, if you ask me.

Anyway, the question for you is: what do the columns really mean? e.g. 1, 12, 123, 1234? You probably also remember your maths teacher telling you that the first column (starting at the right) are ones. The next column (moving left) is tens. Then hundreds. Then thousands.

Hundreds
of
thousands
Tens
of
Thousands
Thousands
Hundreds
Tens
Ones
6
5
4
3
2
1

And that's perfectly true... but there's a little more to it. Each column is actually 10 to the power of 1,2,3,4 etc (commonly expressed as 10^x in cases such as this when I can't use superscripts like this...

This is what we call a base 10 numbering scheme. There are 10 different digits. It's commonly called decimal, "deca" being Latin for ten, as in decade, decahedron and decimate. By the way, did you know 95% of people don't know what 'decimate' means? It does not mean 'annihilate' or wipe out. It means to reduce by one-tenth. The Roman armies used to punish rebellious towns by slaughtering 10% of the population as a warning to the others.

Hundreds
of
thousands
Tens
of
Thousands
Thousands
Hundreds
Tens
Ones
10^5
10^4
10^3
10^2
10^1
10^0
6
5
4
3
2
1
600,000
50,000
4,000
300
20
1

So far so good. We can endlessly create bigger numbers just by adding extra columns to the left, each one being a new power of 10. Using 3 columns, the biggest number we can represent is 999. With 8 columns, we can represent 99,999,999. Etcetera.

But computers don't have ten fingers. To them, counting with 10 digits is as unnatural as, well, us counting with 2 or 16 digits. Digital computers only truly understand two values - on and off. True and false. Electricity on and electricity off. Deep down, they are counting and calculating not with 10 digits (0 to 9) but with 2 (0 to 1). That's a base 2 numbering scheme, or binary, and it goes something like this.

128s
64s
32s
Sixteens
Eights
Fours
Twos
Ones
2^7
2^6
2^5
2^4
2^3
2^2
2^1
2^0
1
0
1
0
0
1
1
1
128
0
32
0
0
4
2
1

Which gives 128 + 32 + 4 + 2 + 1 = 167 in decimal. You're probably thinking "That's pretty dumb, only having 2 digits" but all it means is that to get bigger numbers you just need more columns than decimal uses. In 3 binary digits (111) the biggest number you can represent is 7 (4+2+1). In 8 binary digits, the biggest number you can represent is 255. To get the decimal number 99,999,999 you would need this in binary...

So, the decimal representation is 99,999,999. The binary is 101111101011110000011111111. Not so different, really.

And have you noticed some spooky, familiar numbers appearing above? What about things like 16 (bit computers), 32 (bit operating system), 512 (Meg MP3 player)? Yep. In computers, these numbers turn up all the time because as far as a computer concerned, these are its lovely round numbers. Humans like round numbers like 10 and 100. Digital computers have round numbers like 256 and 1024 since these are powers of 2.

And have you wondered why, when specifying the Red / Green / Blue values for a colour in a web editor or Photoshop that you can use values from 0 to 255? Again - it's because computers are binary, and 255 is the largest number you can store in 8 binary bits. And since a computer's basic unit of storage, the byte, is 8 bits long - hey presto! Mystery solved.

And another thing... we humans think of kilobits and kilobytes as 1000 bits or bytes, but we're rounding off. A 'K' is actually 1024 (again, base 2), so a 56Kbps modem is not rated at 56,000 bits per second - it's 56 x 1024 = 57,344 bps. And a megabyte is not really 1,000,000 bytes - it's 1,048,674 bytes. And a gigabyte isn't 1,000,000,000 bytes - it's 1,073,741,824 bytes. With binary you get that little bit extra as a bonus :-)

Sidenote - So why isn't your new 200G hard disk not reporting 214,748,364,800 bytes free? Because hard disk capacity refer to its unformatted capacity. Formatting (writing the tracks and sectors required to store files on the disk - similar to marking the lanes on an athletics track) reduces the disk's total storage space.

Download my Binary Fun spreadsheet.

So, you're thinking. Binary is a bit of a pain. 27 digits to represent 99,999,999?

And you'd be right. Human brains just don't deal well with lots and lots of digits. Consider this...

Once we get to petabytes, most people's brains turn to mush. That's why when programmers need to talk to computers in terms the computers understand, they don't usually use binary. They use hexadecimal.

 

Hexadecimal

For convenience, programmers usually use base 16 rather than base 2. They used to use base 8 (octal) but that has fallen into disuse. With base 16, you have 16 possible digits compared to decimal's 10. How do we manage to represent 16 digits? By cheating. Instead of inventing new digits to represent 10,11,13,14 and 15 we use the letters A to F.

So, to count in hexadecimal (or 'hex' as it's usually known)... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20...

And to represent 99,999,999 in hex, it would be... 5F5E0FF

Download the Hex Fun spreadsheet

Above you can see the way Photoshop stores RGB colour information. The 3 byte number FFAF5C means Red=FF (255 decimal), Green=AF (175 decimal) and Blue=5C (92 decimal).

Also, you might notice that internet IP addresses take the form 172.95.1.16. This 'dotted decimal' format might be more familiar to you now that you realise the an IP address is 4 bytes long and each byte can be a number from 0 to 255. In fact, the internet would be thinking of the address above as "AB.5F.01.10". Hexadecimal is everywhere in the computer world once you realise what it is.

Back to the IT Lecture Notes index

Back to the last page you visited

Created 30 September 2007

Last changed: September 30, 2007 12:56 PM

IT Lecture notes copyright © Mark Kelly 2001-