Last updated 7 October June 2021

To understand IPv4 addresses and networking you need to understand binary numbers. Put simply, you need to know why the decimal number 255 translates to the binary number 11111111.

Decimal numbers and base-10

In our everyday life we use decimal numbers. If you ask for five apples at your local greengrocer’s then you can be pretty sure that the greengrocer will give you five apples. Similarly, most of us understand that 192 is a three-digit number.

The numbers 5 and 192 are decimal numbers that use the base-10 number system. In the base-10 system there are 10 integers: the range 0 to 9. We are all so familiar with this system that we rarely, if ever, think about the logic behind it.

If you want to explain the system to a space alien or, more likely, computer program, then you could try using a table like this:

 Column | Number | Unit  | Value 
--------+--------+-------+-------
 1      | 1      | 100   | 100   
 2      | 9      |  10   |  90   
 3      | 2      |   1   |   2   
 Total  |        |       | 192   

Here, I split the number 192 into three rows. The first row is for units of 100; the second for units of 10 and the third for units of 1. So, our number is calculated as follows:

(1*100) + (9*10) + (2*1) = 192

The thing to note here is that the Number column can only have values in the range 0-9. If we take the number 200 then the calculation would be like this:

(2*100) + (0*10) + (0*1) = 200

And if we get a smaller or larger number then we just use fewer or more rows for the units. For instance, here we got the number 5:

(5*1) = 5

Exponents (to the power of…)

If the above makes sense then we can next talk about exponents. A number’s exponent indicates how many times to use the number in a multiplication. To illustrate, 102 equals 100 (10*10); 103 equals 1000 (10*10*10) and so forth.

If we return to our dissection of the number of 192 then we can change our table as follows:

 Column | Number | Exponent | Value 
--------+--------+----------+-------
 1      | 1      | 102      | 100   
 2      | 9      | 101      |  90   
 3      | 2      | 100      |   2   
 Total  |        |          | 192   

Instead of using the units 100, 10 and 1 we now use exponents:

 Exponent | Value 
----------+-------
 100      | 1     
 101      | 10    
 102      | 100   
 103      | 1000  

If you are an eagle-eyed reader then you might wonder why 100 returns 1 rather than 0. There is logic behind this and there are lots of different explanations for why 0 always returns 1. The one that makes most sense to me is that exponents represent in how many ways you can write a set of numbers. For instance, 22 equals 4 and there are four possible combinations of the numbers 1 and 2:

  • 1,1
  • 1,2
  • 2,1
  • 2,2

This is true for any exponent, apart from 0. If you have, say, 100 then there is always just one combination:

  • 10

Binary numbers

Base-10 is not the only number system. Computers are rather fond of the binary number system. As its name suggests, this is a base-2 system that uses just two values: 0 and 1. And as you probably know, 0 means “off” and 1 means “on”. There are various other systems. For instance, IPv6 addresses use base-16 (hexadecimals).

Although base-2 works just like base-10 it is a rather confusing system for humans. That is simply because we are used to working with base-10. We know we got the numbers 0 to 9, and we know we can represent a number larger than 9 by using more than one number. It is useful to recap how that works, as the same principle applies to base-2.

The largest number in base-10 is 9. When we need to represent 10 we put a zero in the ones column and a 1 in the tens column. It is that easy!

In base-2 we only got the numbers 0 and 1, and so the largest number is 1. If we want to represent 2 we do the same thing we do in base-10: we put a zero in the right-most column and a 1 in the column to the left. So, the number 2 is represented as 10.

If 2 is 10 then 3 is 11 – the right-most number is a zero and so we can increase it 1. When we get to number four we have run out of numbers, so we change the numbers to zero and add a new column to the left – this gives us 100. You can continue like this forever:

 Base-10 | Base-2 
---------+--------
 0       | 0      
 1       | 1      
 2       | 10     
 3       | 11     
 4       | 100    
 5       | 101    
 6       | 110    
 7       | 111    
 8       | 1000   
 9       | 1001   
 10      | 1010   

Converting to and from binary numbers

Let’s return to the (decimal) number 192. How is the number represented in the binary system?

The trick is to divide the number by the exponent (2) and to then keep dividing the remainder until the quotient is 0. That sounds more complicated than it really is – the following table shows the line-by-line calculation for the number 192:

 R/2   | Quotient | Remainder 
-------+----------+-----------
 192/2 | 96       | 0         
  96/2 | 48       | 0         
  48/2 | 24       | 0         
  24/2 | 12       | 0         
  12/2 |  6       | 0         
   6/2 |  3       | 0         
   3/2 |  1       | 1         
   1/2 |  0       | 1         

Here, the remainder is 00000011. The base-2 number is the remainder in reverse: 11000000.

Converting to base-10

Of course you can also convert a binary number to a base-10 number. For that, we can use a table with two rows. The first row contains exponents and the second binary numbers:

 128 |  64 |  32 |  16 |   8 |   4 |   2 |   1  
-----+-----+:----+-----+-----+-----+-----+-----
   1 |   1 |   0 |   0 |   0 |   0 |   0 |   0  

You can get to 192 by adding up 128 and 64. Both exponents therefore get a 1 in the corresponding field on the second row. All the other binary fields are off. Or, in other words, you can calculate the number as follows:

(1*128) + (1*64) + (0*32) + (0*16) + (0*8) + (0*4) + (0*2) + (0*1) = 192

There are a couple of important things to note about the above binary table. Firstly, the maximum value you can get when you have eight columns is 255: if all the binary bits are on (11111111) then the sum of the numbers is 255.

Secondly, the sum of the numbers before each binary number add up to the binary number minus 1. For instance, all the columns up to the column with the value 8 add up to 7 (1+2+4):

 128 |  64 |  32 |  16 |   8 |   4 |   2 |   1  
-----+-----+:----+-----+-----+-----+-----+------
   0 |   0 |   0 |   0 |   0 |   1 |   1 |   1  

And, to give one more example, this adds up to 127:

 128 |  64 |  32 |  16 |   8 |   4 |   2 |   1  
-----+-----+:----+-----+-----+-----+-----+------
   0 |   1 |   1 |   1 |   1 |   1 |   1 |   1  

Finally, also note that we always use eight binary numbers. Each 0 or 1 is a bit (and the eight bits combined are a byte). An IPv4 address is always made up of four octets separated by dots. Each octet is eight bits and an IPv4 address is therefore always a 32-bit address (4*8). IPv6 addresses are always 128-bit and made up of eight 16-bit hextets (separated by colons).

I hope you can see that there is more to base-2 than just zeros and ones. It is quite a smart system. No wonder computers are so fond of it!