# Binary – What is it and How Can I Use it?

I’m sure everyone’s heard of binary at some point. After all, it is the basis of all computers.

But do you actually know what it is? If I gave you a binary number, could you work out what number that was?

### Number Systems

The first thing we’ll discuss is a number system. What is it?

Well, there are a lot of different number systems to have been used in time; for example, the decimal system we all use today, or the Roman system.

The specific ones we’ll be discussing today are those which use a positional notation, such as decimal, or, as you may have guessed, binary.

A positional numeral system is one where each digit’s contribution to the total number is related to the digit and its position, or index.

For example, in the number 123 in base 10, the three contributes $3 \times 10^0 = 3$. The two represents $2 \times 10^1 = 20$ and the three represents $3 \times 10^2 = 300$.

This differs from those such as the Roman Numeral system which just have each letter representing a single number. For example, I for 1, and V for 5.

### Binary

So, having given an initial explanation of number systems, let’s get to the main point of this article.

Simply put, binary is just a base 2 representation.

This means that there are 2 unique digits for each position in the number.

It also means that each digit contributes the product of its digit and two raised to the power of its position to the total number.

For example, 5 is represented as 101 since $5 = 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0$.

This is very powerful since each bit, a 1 or a 0, can represent on or off, which is means we can use electric signals.

### Other Systems

There are other positional systems. As I’ve already said, the decimal system which is commonly used worldwide is just base 10.

Similarly, you could have base 16, hexadecimal, where each digit represents a power of 16.

Since for this we would need 16 different digits, hexadecimal uses the numbers 0-9 as well as the letters A-F for the numbers 10-15.

You may have seen this when choosing a colour for something; for example: 4B5 = $4 \times 16^2 + 11 \times 16^1 + 5 \times 16^0 = 1205$.

You can see why this might be useful; it requires fewer digits in general to represent a number in hexadecimal than it would in decimal or binary.

As you might know, the Babylonians used sexagesimal numbers, i.e. base 60.

This is still used today for some aspects; if you think about it, you would write an amount of time as 2:13:24 (2 hours, 13 minutes and 24 seconds.)

Without thinking, to convert this into seconds, you would do:

$2 \times 60^2 + 13 \times 60^1 + 24 \times 60^0 = 8004$ seconds.

For a list of numeral systems, take a look at this Wikipedia article.

### Non Integers

So far, we’ve only talked about integers, but binary can also represent non-integers.

This is quite simple really, after the point, the same pattern continues, and the powers of 2 keep repeating.

For example, 0.101 would represent $1 \times 2^{-1} + 0 \times 2^{-2} + 1 \times 2^{-3} = 0.625$.

Typically, non-integers are known as ‘floats’ or ‘doubles’ when programming.

### Storing Binary

In a computer program, the computer will store numbers in a fixed number of bits.

For example, it may store an integer in 8 bits, meaning there are 8 ones and zeros. In this case, it would store 7 as 00000111.

This means that, for integers, the range of available numbers is $0$ to $2^n – 1$ where $n$ is the number of bits.

Increasing the number of bits used after the point in a float will just act to increase the accuracy.

This is exactly the same as using fewer numbers after a decimal point by rounding up or down.

### Negative Numbers

Normally, we would use a negative sign to denote a negative number, but in binary, we only have a 1 or a 0.

It might be tempting to suggest that we use one bit just to say whether the number is positive or negative.

While this is intuitive, when it comes to adding or subtracting binary, we would have to use different algorithms when adding or subtracting

This is annoying since we would like to be able to only have one algorithm.

In addition, in this representation, we have a positive zero, 0000, and a negative zero, 1000. Which should we use?

#### Two’s Complement

To get over these obstacle, we can use something called “Two’s Complement”.

This still reserves the first bit only for negative numbers, but it does so in a different way.

A binary number in this representation with a 1 for the first bit is negative, and a positive number has a 0 in the first bit.

For an n bit integer, there will still be $2^n$ numbers which can be represented.

However, now, there are effectively only $n-1$ bits for positive numbers.

So, the range of available numbers for a Two’s Complement $n$ bit number is: $- 2^{n-1}$ to $2^{n-1} -1$.

To represent a negative number in Two’s Complement, invert all the bits, and then add 1 to the resulting number.

For example, to represent the number -9 using 8 bits, we:

1. Write out 9 in binary as 00001001.
2. Invert all the bits to obtain 11110110.
3. Add 1 to obtain 11110111.

So, -9 is represented as 11110111, in 8-bit.

To find out what a Two’s Complement negative number represents, you can perform the same operation as before and interpret this as a magnitude, adding a negative sign.

So, given the number 11110111 in 8-bit Two’s complement, we:

1. Invert all the bits to obtain 00001000.
2. Add 1 to obtain 00001001.
3. Thus we interpret 11110111 as – (00001001)

### Working with Binary

Now we’re going to talk a bit about adding and subtracting binary numbers. If you’d like to try some exercises yourself, then you can check them using this calculator courtesy of calculator.net.

Luckily, our representation of negative numbers will mean that we can perform the same operation regardless of addition or subtraction.

Adding binary numbers is very simple, all you need to know is this:

1. 0 + 0 = 0
2. 1 + 0 = 1
3. 1 + 1 = 10

Then, you can add two numbers as you would normally where adding a 1 and a 1 means you carry a 1 to the digit to the left.

For example, 1011 + 1001:

By carrying ones at each stage of the calculation, and following the rules, we find that 1011 + 1001 = 10100, which is equivalent to 11 + 9 = 20.

#### Subtraction

There are two ways to subtract binary numbers, but I am going to give you the easiest one.

Firstly, you’ll need to convert the number being subtracted into its negative Two’s Complement form.

You then add the two numbers as you did in the previous section.

For example, 1001 – 1011 would be written as 00001001 + 11110101.

If we end up with a 1 in an additional bit to the left, then you can just ignore it.

The result that you get will be in Two’s Complement form.