When it comes to computers there are several different number systems that are commonly used. These are binary, octal, decimal and hexadecimal (often shortened to hex). You may recognise decimal as that is what we learn’t in school but the others are not used as much.

As you use it all the time I wont spend much time on decimal which is base 10 (0-9).

**Binary or base 2 (0-1)**

Binary is the language of computers, anything you put into a computer is then translated into binary.

**Octal or base 8 (0-7)**

Octal used to be used in the early days of computing when they had a lot less space to work with, but now it is not used as much.

**Hexadecimal or base 16 (0-f)(0-9 + A-F)**

As the numbers we use go from 0-9 letters had to be used to represent the last few numbers.

If you are going to learn another number system it should be hexadecimal as this is what is often used to represent colours when programming, and as a compact way to represent binary numbers.

**Converting between binary and decimal**

To convert decimal to binary you subtract using powers of two ( 1, 2, 4, 8, 16, 32, 64 etc.).

as an example I will change the number 23 to binary.

- 23-32 = cant (0)
- 23-16 = 7 (1)
- 7-8 = cant (0)
- 7-4 = 3 (1)
- 3-2 = cant (1)
- 1-1 = 0 (1)

At step 1 the number we are looking to subtract (32) is larger than our starting number (23), as we cant subtract 32 without going into negative numbers we set the digit to zero and carry the 23 over to step 2.

At step 2 we subtract 16 from 23 resulting in 7 as this is possible we set the digit to 1, the result is then carried over to step 3.

You continue doing this until you reach the last digit.

The result: 23 = 010111

To go the other way binary to decimal using the binary number we ended up with in the previous example we multiply and add instead of subtract.

0 x 32 = 0

1 x 16 = 16

0 x 8 = 0

1 x 4 = 4

1 x 2 = 2

1 x 1 = 1

You first multiply by the powers of 2.

0 + 16 + 0 + 4 + 2 + 1 = 23

We then add these together to get our decimal value.

010111 = 0 + 16 + 0 + 4 + 2 + 1 = 23

**Negative numbers in binary**

Binary numbers can be signed (positive and negative) or unsigned (positive only).

For an 8 bit binary number:

Unsigned | ||
---|---|---|

Base | Min | Max |

binary | 00000000 | 11111111 |

decimal | 0 | 255 |

Signed | |||
---|---|---|---|

Base | Min | Max | |

binary | 10000000 | 00000000 | 01111111 |

decimal | -128 | 0 | 127 |

Signed numbers start with a 0 for positive and 1 for negative, so 010111 as a signed number would be positive.

To swap a signed binary number value from positive to negative or from negative to positive two’s compliment is used which is as simple as switching all the one’s to zero’s and zero’s to one’s then adding one as shown below.

starting with: 010111

101000 + 1 = 101001

and back again

010110 + 1 = 010111

In my next post I will demonstrate how to do the same as above for octal and hexadecimal .