Binary and hexadecimal

| | Comments (0)

This page attempts to teach what binary and hexadecimal numbers are all about.

Ten fingers

We count in base ten, also known as decimal or denary. “Counting in base ten” means that we count up to nine, and then use double digits: 10.

That number is not a single digit, but a compound number. It means one ten plus zero ones. Another example: 237 means two hundreds plus three tens plus seven ones. Simple.

Each additional digit is another multiple of ten. The right-most digit represents ones, then the next is tens, then hundreds (10×10), then thousands (10×10×10) and so on. The nth digit represents 10 multiplied by itself n−1 times, otherwise known as 10 to the power (n−1).

The reason we count in base ten is because we have ten fingers. There is no magic to the number ten. If we had eight fingers, we would count in base eight, or octal. We would count thus: 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20 etc. The number 10 (one-zero) would represent a numerical value of eight. We would have developed different words for numbers, as our number names are designed around base ten.

So the octal number 237 is: two lots of sixty-four (8×8), three lots of eight and seven ones. Add that together and you get a decimal value of 128+24+7=159.

You can count in bases larger than ten, too. We don’t have single digits for values greater than nine, so we use letters. In base thirteen, we would count thus: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 20 etc.

Let’s convert the decimal number 193 into base thirteen. The first three place values in base thirteen are 1, 13 and 169 (13×13). We can obviously fit 169 into 193, but only once. So the base thirteen number is 1XX, leaving us with (193−169=)24. We can fit one thirteen into that, but not two (26). So we have 11X with 11 left over (24-13). The digit for 11 is B (…9, A, B…), so the base thirteen representation of 193 is 11B.

On and off

Computers work in binary: base two. This is because they use switches rather than fingers, and a switch is either on or off. Base two counts up to one, and then goes to double digits: 1, 10, 11, 100, 101, 110, 111, 1000 etc.

The place values are multiples of two: 1, 2, 4, 8, 16, 32, 64, 128 etc. So we can convert to binary: 29, for example, is 16+8+4+1, or 11101 in binary. We can convert back, as well: 1010101 is 64+16+4+1, or 85.

In binary, a digit is called a bit (short for binary digit), and is abbreviated as b. Four bits is occasionally called a nybble. Eight bits make a byte, abbreviated B. Larger values have ambiguous meanings: a kilobyte is either 1000 bytes (kB) or 1024 bytes (KB). 1024 is 2 to the power 10. A megabyte is either 1000000 bytes (mB) or 1048576 bytes (MB). 1048576 is two to the power 20.

Sweet sixteen

Binary numbers are lengthy and hard for humans to work with; decimal numbers are awkward for computers. A good compromise is to count in base sixteen: hexadecimal. The place values are 1, 16, 256, 4096 etc.; the digits used are 0 to F.

Because sixteen is two to the power four, there is a nice relationship between binary and hexadecimal. Each hexadecimal digit takes four bits to represent, so you can split a binary number up into nybbles and convert each one separately, without having to calculate the value of the whole number. Let’s look at an example:

binary:  1010 0010 1111 1001 0001 0000 0100
hex:       A    2    F    9    1    0    4

Here, the binary number 1010001011111001000100000100 is converted to the more manageable hex number A2F9104. We did not have to calculate its decimal value to do the conversion, although you can do that as an exercise if you want. Hint: it’s 170889476.

Bytes are therefore represented by two hex digits, ranging from 00 (zero) to FF (255).

Where next?

Search the web:

Entry sequence:

Site contents:

Other entries in category:

Other entries tagged:

Leave a comment

About this entry

This entry was posted on 2 August 2005 at 20:30.

No more ouch — dental anaesthesia was the previous entry in this blog.

Fake memory cards is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Flickrings

August 2008

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31