Sub topic 6
Adding, Subtracting and Multiplying in Other Bases
This chapter assumes you can convert between decimal and base n (the previous two chapters), do column arithmetic in decimal, and write a number as a sum of digits times powers of the base. By the end you should be able to do column arithmetic directly in any base and check the result quickly.
The working model: same algorithm, different carry
Column arithmetic in base 10 works because each column is worth ten times the column on its right. In base n, each column is worth n times the column on its right. Every rule you know carries over if you replace "10" with n:
- Addition: if a column total is n or more, write the total minus n (the remainder) and carry 1 (the quotient) to the next column.
- Subtraction: if the top digit is smaller than the bottom digit, borrow 1 from the next column. That borrowed 1 is worth n in the current column, not 10.
- Multiplication: multiply digit by digit. Split each product into a quotient (the carry) and a remainder (the digit you write).
Only the digits 0 to n−1 may appear in a numeral. If a base-5 answer contains a 5, you forgot to carry.
When a column total is large (in multiplication it can reach several times n), carry the quotient. For example, 17 in base 5 is 3 fives and 2 left over, so you write 2 and carry 3.
Addition
Add 243 and 134 in base 5, working from the right.
- Units: 3 + 4 = 7. Since 7 = 1·5 + 2, write 2 and carry 1.
- Fives: 4 + 3 + 1 = 8. Since 8 = 1·5 + 3, write 3 and carry 1.
- Twenty-fives: 2 + 1 + 1 = 4. Write 4.
So 243₅ + 134₅ = 432₅. In decimal, 243₅ = 73 and 134₅ = 44. Their sum is 117, and 432₅ = 4·25 + 3·5 + 2 = 117.
Hexadecimal works the same way, with A to F standing for 10 to 15. For 2F₁₆ + 1B₁₆, the units column gives 15 + 11 = 26 = 1·16 + 10. Write A and carry 1. The next column gives 2 + 1 + 1 = 4. The answer is 4A₁₆, which is 47 + 27 = 74 in decimal.
Binary is the simplest case, because the only carry rule is 1 + 1 = 10₂. For 1011₂ + 110₂, the carry propagates through every column and the result is 10001₂. In decimal that is 11 + 6 = 17.
Subtraction
Subtract 254₇ from 536₇.
- Units: 6 − 4 = 2.
- Sevens: 3 − 5 is not possible. Borrow from the next column, which turns the 3 into 3 + 7 = 10. Then 10 − 5 = 5. The leading 5 becomes 4.
- Forty-nines: 4 − 2 = 2.
So 536₇ − 254₇ = 252₇. Check: 536₇ = 272, 254₇ = 137, and 272 − 137 = 135 = 2·49 + 5·7 + 2.
The usual trap is borrowing "10". In base 7 a borrow adds 7 to the digit.
Borrowing across zeros is where students slip. Take 1000₅ − 1₅. Each zero turns into a 4 (the largest digit) as the borrow passes through it, and the last column becomes 5 − 1 = 4. The answer is 444₅. In decimal that is 125 − 1 = 124 = 4·25 + 4·5 + 4. This matches base 10, where 1000 − 1 = 999.
Multiplication
Multiply 45₆ by 23₆. Use the same long-multiplication layout as in decimal, with one row per digit of the multiplier.
First row, 45₆ × 3:
- 5 × 3 = 15 = 2·6 + 3. Write 3 and carry 2.
- 4 × 3 = 12, plus the carry 2, is 14 = 2·6 + 2. Write 2 and carry 2.
- Write the final carry, 2.
The row is 223₆.
Second row, 45₆ × 2, shifted one place left:
- 5 × 2 = 10 = 1·6 + 4. Write 4 and carry 1.
- 4 × 2 = 8, plus the carry 1, is 9 = 1·6 + 3. Write 3 and carry 1.
- Write the final carry, 1.
The row is 134₆, and shifted it becomes 1340₆.
Now add the rows in base 6: 223₆ + 1340₆.
- Units: 3 + 0 = 3.
- Sixes: 2 + 4 = 6 = 1·6 + 0. Write 0 and carry 1.
- Thirty-sixes: 2 + 3 + 1 = 6. Write 0 and carry 1.
- Two-hundred-sixteens: 1 + 1 = 2.
The result is 2003₆. Check: 45₆ = 29, 23₆ = 15, and 29 × 15 = 435 = 2·216 + 3.
Two facts speed up this work:
- Multiplying by the base shifts the digits left. For example, 23₅ × 10₅ = 230₅.
- Building a small times table for the base before you start helps when the same digit repeats. Your decimal table works if you convert each product to base n by dividing by n.
Native arithmetic or convert to decimal?
You can solve any of these problems two ways: work in the base, or convert to decimal, compute, and convert back. Neither always wins.
| Situation | Better method |
|---|---|
| Short numbers (2 to 4 digits) and the answer is needed in the base | Work in the base |
| Binary or hexadecimal with small digits | Work in the base |
| Long numbers or an awkward base such as 7 or 9, with only a decimal result needed | Convert to decimal |
| Only a yes/no or comparison is needed | Convert to decimal |
| Several operations in a row, answer in the base | Work in the base for each step, then check in decimal |
Converting back from decimal costs a repeated-division step, so for an answer in base n with short operands, native arithmetic is usually faster.
Checking with the digit sum in base n − 1
In base 10, casting out nines relies on 10 leaving remainder 1 when divided by 9. The same argument works in any base: n leaves remainder 1 when divided by n − 1. So a base-n numeral leaves the same remainder mod (n − 1) as its digit sum.
Apply this to the base-5 sum earlier, using mod 4:
- 243₅ has digit sum 9, and 9 mod 4 = 1.
- 134₅ has digit sum 8, and 8 mod 4 = 0.
- The expected sum has remainder 1 + 0 = 1.
- 432₅ has digit sum 9, and 9 mod 4 = 1. ✓
For the base-6 product, use mod 5. 45₆ has digit sum 9, giving remainder 4. 23₆ has digit sum 5, giving remainder 0. The product should have remainder 4 × 0 = 0. And 2003₆ has digit sum 5, giving remainder 0. ✓
Verifying in code
The functions below do column addition the way you do on paper. The code then checks the results against Python's own conversion, int(text, base), which accepts bases 2 to 36.
SYMBOLS = "0123456789ABCDEF"
def to_base(n, base):
if n == 0:
return "0"
out = []
while n > 0:
n, r = divmod(n, base)
out.append(SYMBOLS[r])
return "".join(reversed(out))
def add_base(a, b, base):
a, b = a[::-1], b[::-1]
out, carry = [], 0
for i in range(max(len(a), len(b))):
da = SYMBOLS.index(a[i]) if i < len(a) else 0
db = SYMBOLS.index(b[i]) if i < len(b) else 0
carry, digit = divmod(da + db + carry, base)
out.append(SYMBOLS[digit])
if carry:
out.append(SYMBOLS[carry])
return "".join(reversed(out))
print(add_base("243", "134", 5)) # 432
print(add_base("2F", "1B", 16)) # 4A
print(add_base("1011", "110", 2)) # 10001
# Subtraction and multiplication via decimal, as a cross-check
print(to_base(int("536", 7) - int("254", 7), 7)) # 252
print(to_base(int("45", 6) * int("23", 6), 6)) # 2003
print(to_base(int("1000", 5) - int("1", 5), 5)) # 444Common errors
- Carrying or borrowing 10. In base n the amount is n. This is the most frequent mistake.
- Leaving an invalid digit. A result such as 6₆ or 9₈ means a carry was missed.
- Forgetting the final carry. The answer can have one more digit than either operand, as in 1011₂ + 110₂.
- Mixing representations. Do not combine a base-7 numeral with a decimal one in the same column. Convert everything to one base first.
- Answering in the wrong base. If the question asks for the result in base 8, a correct decimal value is still a wrong answer.
- Reading numerals as decimal. 100₅ is 25, not one hundred. Subscripts matter when you compare sizes.
Exercise
Compute 345₈ + 267₈ directly in base 8, and confirm the result with a decimal check.
Show answer
Units: 5 + 7 = 12 = 1·8 + 4, so write 4 and carry 1. Eights: 4 + 6 + 1 = 11 = 1·8 + 3, so write 3 and carry 1. Sixty-fours: 3 + 2 + 1 = 6. The answer is 634₈. Check: 345₈ = 229 and 267₈ = 183, which sum to 412, and 634₈ = 6·64 + 3·8 + 4 = 412.
Exercise
Compute 1000₂ − 11₂ in binary, borrowing in binary.
Show answer
Borrow through the zeros. The three zeros become 1, 1, 1 after passing the borrow, but the units column becomes 10₂ (value 2), so 10₂ − 11₂ cannot be done directly. Instead rewrite 1000₂ as 111₂ + 1 and do it in decimal: 8 − 3 = 5 = 101₂. In columns, the result is 101₂.
Exercise
Multiply 2D₁₆ by 3 in hexadecimal, working in base 16, and give the answer in hexadecimal.
Show answer
D = 13, and 13 × 3 = 39 = 2·16 + 7, so write 7 and carry 2. Then 2 × 3 = 6, plus the carry 2, is 8. The answer is 87₁₆. Check: 2D₁₆ = 45, and 45 × 3 = 135 = 8·16 + 7.