ScienceComputer Science

Number Base
arithmetic.

Add, subtract, multiply or divide in binary, hex or any base up to 36, with the steps.

How it's worked out

In base bb, each digit is worth its own value times a power of bb, counted from 0 at the right. Digits above 9 are letters: A is 10, B is 11, and Z is 35.

N\displaystyle N=dkbk+⋯+d1b+d0\displaystyle {}= d_k b^{k} + \dots + d_1 b + d_0

The paper methods from school work in every base. Only the point where a column overflows changes. OpenStax puts it simply: set the sum up as in base 10 and use the addition table for the base. In hex, A9 + 94 adds 9 + 4 = 13, which is the digit D. Then A + 9 = 19, which is 16 + 3, so you write 3 and carry 1. The answer is 13D.

Subtraction borrows one of the base, not 10, from the next column. Multiplication writes one row per digit of the second number, each moved one place left, then adds the rows in the base. Long division brings down one digit at a time and asks how many times the divisor fits.

The tool reads both numbers as exact whole numbers of any length, does the sum, and writes the answer in the base you pick. It then checks the result in decimal and by undoing the operation.

Worked examples

Sam checks a hex product from a register map

Sam needs 1A × B in hex. B is one digit, so there is one row: 11E. In decimal that is 26 × 11 = 286, and 286 is 11E in hex.

The undo check divides 11E by B and gets 1A back. OpenStax works a two-row product the same way: 3A × 74 in base 12 is 2414.

Open this example in the tool

Ana works a base 6 subtraction with a borrow

In the ones column 2 is less than 4, so Ana borrows one six from the 5. The ones digit becomes 2 + 6 = 8, and 8 − 4 = 4. The 5 is now 4, and 4 − 3 = 1.

So 52 − 34 = 14 in base 6. In decimal that is 32 − 22 = 10, which matches.

Open this example in the tool

A base 5 long division with a remainder

23 in base 5 does not go into 4, so the first quotient digit is 0. It goes into 43 once, leaving 20. Carry on bringing down digits and the quotient is 140 with remainder 1.

Check in decimal: 586 ÷ 13 = 45 remainder 1, and 45 is 140 in base 5.

Open this example in the tool

Reading your result

The answer is in the base you choose, with the decimal value beside it. Division gives a whole-number quotient and a remainder. Every result is exact, so FFFFFFFFFFFFFFFF + 1, one more than the largest 64-bit value, comes out as 18,446,744,073,709,551,616 in decimal with no rounding.

The facts table answers the lookup that slows people down. In base 5, 3 + 4 = 12 and 3 × 4 = 22. In octal, 7 × 7 = 61. A carry happens whenever a fact has two digits.

Negative numbers follow the usual sign rules. 5 + (−9) is worked as 9 − 5 and given a minus sign, so it is −4. Division rounds toward zero, so −7 ÷ 2 is −3 remainder −1.

Hex offsets are the everyday case for programmers. A buffer at address 7FF0 with an offset of 20 hex (32 bytes) ends at 8010. That one sum carried 2 times, which is the step people get wrong when they add hex in their head.

Binary follows the same rules. 1001011 ÷ 11 is 11001 with remainder 0, which is 75 ÷ 3 = 25 in decimal.

Common mistakes

  • Carrying at 10. In octal, 7 + 1 fills the column, so 777 + 1 = 1000. If you carry only at 10, you write 778, and 8 is not even an octal digit.
  • Borrowing 10. A borrow adds the base. In base 6 the borrowed column gets 6 more, not 10.
  • Reading 10 as ten. In any base, 10 means one of the base and nothing more. It is 2 in base 2, 8 in base 8, 16 in base 16.
  • A digit that does not exist. G is not a hex digit and 5 is not a base 5 digit. The boxes refuse them, but on paper they slip in when a fact is misremembered.
  • Mixing bases. Both numbers must be in the same base. Convert one first if they start in different ones.

Questions people ask

Borrow 1 from the next column to the left, which adds 16 to the column you are working on. A − F becomes 26 − 15 = 11, which is B, and the digit you borrowed from goes down by 1.

Sources