Sub topic 6
Adding, Subtracting and Multiplying in Other Bases
This chapter assumes you know place value, powers of a base, division with remainders, and basic decimal arithmetic. You can use these operations in any base. The main difference is that a column can hold only digits from 0 through one less than the base.
The working rule for carrying
In base \(b\), a column total of \(b\) becomes 0 in that column and carries 1 to the next column. A total of \(b+r\) becomes digit \(r\) and carries 1.
For example, in base 5, \(7=12_5\). The digit 2 stays in the current column, and 1 is carried.
Consider:
\[ 2435+1345 \]
Work from right to left.
| Column | Calculation | Write | Carry |
|---|---|---|---|
| Ones | \(3+4=7\) | 2 | 1 |
| Fives | \(4+3+1=8\) | 3 | 1 |
| Twenty-fives | \(2+1+1=4\) | 4 | 0 |
Therefore,
\[ 2435+1345=432_5. \]
A decimal check gives \(73+44=117\), and \(432_5=4(25)+3(5)+2=117\).
Subtraction and borrowing
Borrowing works differently in appearance but follows the same place-value idea. Borrowing 1 from the next column gives \(b\) units in the current column.
For example:
\[ 4025-1345 \]
The ones column has \(2-4\), so it needs a borrow. The fives column contains 0, so borrow first from the twenty-fives column. This changes the 4 to 3 and gives 5 fives. Then borrow one five for the ones column.
The ones column becomes \(7-4=3\). The fives column becomes \(4-3=1\). The twenty-fives column becomes \(3-1=2\).
Thus,
\[ 4025-1345=213_5. \]
The decimal check is \(102-44=58\), and \(213_5=2(25)+1(5)+3=58\).
Multiplication in another base
Use the usual long-multiplication layout. Each partial product must be reduced to valid base-\(b\) digits. Moving one place left means multiplying by the base, so append one zero to the shifted partial product.
Calculate:
\[ 235\times145 \]
First multiply by 4:
\[ 235\times4=2025. \]
The 1 in \(14_5\) represents one group of five, so the second partial product is:
\[ 235\times1\times5=2305. \]
Add the partial products:
\[ 2025+2305=432_5. \]
A decimal check gives \(13\times9=117\), and \(432_5=117\).
DIGITS = "0123456789ABCDEF"
def add_base(left, right, base):
carry = 0
result = []
for i in range(max(len(left), len(right))):
a = DIGITS.index(left[-1 - i]) if i < len(left) else 0
b = DIGITS.index(right[-1 - i]) if i < len(right) else 0
total = a + b + carry
result.append(DIGITS[total % base])
carry = total // base
if carry:
result.append(DIGITS[carry])
return "".join(reversed(result))
print(add_base("243", "134", 5)) # 432Checking answers quickly
Use these checks:
- Every digit must be less than the base.
- Convert both sides to decimal when the numbers are small.
- Estimate size. Adding two three-digit base-5 numbers should not produce an unexpectedly long result.
- For multiplication, check that the product is larger than either positive factor.
Exercise
Calculate \(3146-1256\).
Show answer
\(3146-1256=1456\). In decimal, \(118-47=71\), and \(1456=1(36)+4(6)+5=65\). Correction: the subtraction is \(1516\), because \(118-47=71\) and \(1516=71\).