# Introduction to Computer Arithmetic (Changing Bases, Sums & Multiplication)

This post is going to be an intorduction to computer arithmetic and will give us a bit of background of what bases are and how to switch between them without first converting to base 10 (which is how our brains usually deal with numbers). We’re also going to look at performing sums with different bases without first changing to base 10 (great fun).

## Changing bases

When is comes to computer arithmetic, we need to be able to play with changing bases. We usually see and use numbers that are in base 10 (denary / decimal) but we need to be able to change these to other bases. In computer science, a lot of the time we’re performing calculations in base 2 and sometimes base 8 (octal) or even base 16 (hexadecimal) but we can change the base to any number. We can also come across base 3 or base 9 so understanding how this principle of switching between bases is essential to our craft.

We also need to be able to convert between bases without first converting to decimal and the first part of this post is going to be looking at the principles of how we perform these conversions using a pen and paper as calculators don’t do these base conversions for us.

When dealing with decimal (base 10), we can break down a number as shown below.

**Base 10**

is , is , is , units are etc. As shown below:

**Base 8**

The same applies to whatever base we’re dealing with so if we’re playing with base 8 then:

**Base n**

Whatever base we’re dealing with is that number raised to the powers:

**Hexadecimal (Base 16)**

Say we were dealing with base 16 (hexadecimal), the units we would be dealing with would be from to (like when we are dealing with base 10 we use the units to ). We can’t have double digits in our numbers because it would be confusing.

For example, is the number really or is is or is it ?

To avoid confusion, letters are used in numbers over 9 to represent the numbers:

and

In this way the numbers become distinct so really would be and would be and would be .

**Converting to Decimal (Denary)**

To understand the principle of how we convert between bases, a nice and easy example is to convert to decimal:

As has a subscript of , we know we’re dealing with base 16.

As has a subscript of , we know we’re dealing with base 8 or an octal number. Point to note is that the “point” in the original number is not a decimal point but rather an octal point.

It is easy when converting numbers to decimal because it is how we have been taught to think when it comes to numbers.

**Converting from Decimal**

Converting to decimal is straight forward because our brains think this way naturally so if we want to convert from decimal to another base we usually need to work this out with pen and paper.

To convert from decimal to binary we can use a process called repeated division.

remainder (these are the units i.e. )

remainder (these are the ‘s)

remainder (these are the ‘s)

remainder (these are the ‘s)

remainder (these are the ‘s)

When we no longer have any numbers to deal with (i.e. we get a zero as an answer from our division) we can stop and we read the number of remainders from the bottom to the top because our units were the first thing we worked out.

This process is a little easier to understand when we convert from decimal to another base.

Octal

remainder (these are the units i.e. )

remainder (these are the ‘s)

remainder (these are the ‘s)

**Converting between bases (Binary/Octal/Hexidecimal)**

The next thing to look at is how we convert between bases without converting to base 10 in the middle. When speaking about binary octal and hexidecimal we can easily do this because the base numbers are related i.e. is and is .

We can say that is bit binary and is bit binary. Bits in binary are like digits in base 10 (we only need 3 binary digits to represent the numbers from and we need binary digits for numbers between as shown in the table below).

We can go from base 8 and base 16 into base 2 simply by reversing this process and we can convert from base 8 into base 16 by converting into binary and then applying the same process and vice versa. We can therefore convert base 8 into base 16 by going through base 2 and not base 10.

Let’s say that we have a binary number that we wanted to convert into octal. We know that is so starting from the units end we split the binary number into groups of three.

is is is

If we wanted to convert base 2 into hex then we would split the number into groups of 4.

is is

**Doing sums in octal/hex without changing to base 10**

Let’s start with an addition example in base 8.

The main thing to remember is that we’re dealing in base 8, so unlike regular addition (where the numbers that we add together add up to 10) our numbers in each of our columns add up to 8.

Starting from the units end, and is (not ) with a left over. We leave the units we got and carry over the remaining (as shown above). We then add the giving us with nothing left over and then the giving us with nothing left over. When we come to the last column we have which is the equivalent as getting a 10 in base 10 so we need to leave a and carry of the ‘s over. Lastly we have of the ‘s left over giving us a total of

Now that we’ve got addition down we can look at subtraction.

When looking at the first column we have a which doesn’t work so we need to carry an from the next column giving us which is . When we get to the next column we find that we have which we can’t do so we need to grab an from the next column (reducing that column by a value of ). Once we’ve done that, in the second column we have take away giving us . Finally we have (which used to be ) take away , leaving us .

## **Doing multiplication in octal without changing to base 10**

Lastly we need to look at multiplication in base 8. This is the same way that we would perform multiplication problems in hex, only we’re using octal as the base for the example.

To make this a bit easier to see, we’re going to break down the problem into two parts and do multiplication of the two columns as usual, only one at a time; starting with the column.

but because we’re working in base 8 we need to realise that we’ve got so we need to put a in the answer slot (as opposed to the that we would put if we were dealing with base 10) and carry the into the next column. Now we need to calculate which gives us . is so we leave the and carry a . As we’ve finished multiplying all the numbers in that row we can just put that remaining in the next column.

Now that we’ve multiplied the ‘s we can multiply the ‘s.

For this, as in normal multiplication we first add a , then we multiply the by which gives us . Then we multiply the by which gives us which in base 8 is giving us a and a to carry over, and because we’re finished there we just put that 1 on the end.

Finally, we’re left with an addition sum to complete the calculation.

To do this we first add the with the which gives us . We’ve then got which gives us . In base 8, goes into once so we carry the with left over. Then we’ve got a plus the carried over giving us and lastly the plus nothing gives us .

That’s it for the introduction to computer arithmetic. In this post we covered changing between bases and addition and multiplication in different bases. Hope it was useful for you. It should cover the basics of everything you need to know to get started on some more advanced stuff (which I write a post about in the future).