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.

2643.2 = {2}\cdot{1000} +  {6}\cdot{100} +  {4}\cdot{10} +  {3}\cdot{1} +  {2}\cdot{0.1}

Base 10

 1,000 is  10^3,  100 is  10^2,  10 is  10^1, units are  10^0 etc. As shown below:

..., 10^3,  10^2, 10^1, 10^0.10^{-1}, 10^{-2}, ...

Base 8

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

..., 8^3,  8^2, 8^1, 8^0 . 8^{-1}, 8^{-2}, ...

Base n

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

..., n^3,  n^2, n^1, n^0 . n^{-1}, n^{-2}, ...

Hexadecimal (Base 16)

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

For example, is the number  1123 really  1123 or is is  (11)23 or is it  1(12)3?

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

 A=10, B=11, C=12, D=13, E=14 and  F=15

In this way the numbers become distinct so  1123 really would be  1123 and  (11)23 would be  B23 and  1(12)3 would be  1C3 .

Converting to Decimal (Denary)

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

 1A3_{16} = ({1}\cdot{16^2}) +  ({10}\cdot{16^1}) +  ({3}\cdot{16^0}) = 419

As  1A3_{16} has a subscript of 16, we know we’re dealing with base 16.

 457.2_{8} = ({4}\cdot{8^2}) +  ({5}\cdot{8^1})  +  ({7}\cdot{8^0})  +  ({2}\cdot{8^{-1}}) = 303.25

As   457.2_{8}  has a subscript of 8, 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.

 10110_{2} = ({1}\cdot{2^4}) +   ({0}\cdot{2^3}) + ({1}\cdot{2^2}) +  ({1}\cdot{2^1}) +  ({0}\cdot{2^0}) +  = 22

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.

\frac{29}{2} = 14 remainder 1 (these are the units i.e. 2^0)

\frac{14}{2} = 7 remainder 0 (these are the 2^1‘s)

\frac{7}{2} = 3 remainder 1 (these are the 2^2‘s)

\frac{3}{2} = 1 remainder 1 (these are the 2^3‘s)

\frac{1}{2} = 0 remainder 1 (these are the 2^4‘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.

29_{10} \rightarrow 11101_2

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

157_{10} \rightarrow Octal

\frac{157}{8} = 19 remainder 5 (these are the units i.e. 8^0)

\frac{19}{8} = 2 remainder 3 (these are the 8^1‘s)

\frac{2}{8} = 0 remainder 2 (these are the 8^2‘s)

157_{10} \rightarrow 235_8

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. 16 is 2^4 and 8 is 2^3.

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

  1 {0} 001
  2 {0} 010
  3 {0} 011
  4 {0} 100
  5 {0} 101
  6 {0} 110
  7 {0} 111
  8 1000
  9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

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 8 is 2^3 so starting from the units end we split the binary number into groups of three.

1101011_2 \rightarrow 001 is 1 | 101 is 5 | 011 is 3 \rightarrow 153_{8}

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

1101011_2 \rightarrow 0110 is 6 | 1011 is B \rightarrow 6B_{16}

Doing sums in octal/hex without changing to base 10

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

4013_8 + 4217_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.

4 0 1 3
4 2 1 7 +
1 0 2 3 2
1 1

Starting from the units end, 7 and 3 is 8 (not 10 ) with a 2 left over. We leave the 2 units we got and carry over the remaining 1\cdot8 (as shown above). We then add the 1+1+1 giving us 3 with nothing left over and then the 2+0+0 giving us 2 with nothing left over. When we come to the last column we have 8+8 which is the equivalent as getting a 10 in base 10 so we need to leave a 0 and carry 1 of the 8‘s over. Lastly we have 1 of the 8‘s left over giving us a total of 10,232_{8}

4013_8 + 4217_8 = 10,232_8

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

213_8 - 45_8

\xout{2} 1 \xout{1} 8 8+3
4 5 -
1 4 6

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

213_8 - 45_8=146_8

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.

43_8 \cdot 27_8

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 7 column.

4 3
2 7 \times
3 6 5
3 2

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

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

4 3
2 7 \times
3 6 5
1 0 6 0

For this, as in normal multiplication we first add a 0, then we multiply the 2 by 3 which gives us 6. Then we multiply the 2 by 4 which gives us 8 which in base 8 is 10 giving us a 0 and a 1 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.

4 3
2 7 \times
3 6 5
1 0 6 0 +
1 4 4 5
1

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

\therefore 43_8\cdot27_8=1445

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).

Leave a Comment

Your email address will not be published. Required fields are marked *