Sub topic 3
From Base n to Decimal
What this chapter assumes
You should know powers of 2, 3 and 10, and be comfortable with basic algebra. The previous chapters covered place value and how to write a numeral in base n. This chapter turns a numeral written in base n into an ordinary decimal number. CAT questions use this step constantly, either directly or as the first step of a longer problem.
The expansion rule
A numeral in base n is a shorthand for a sum. If the digits are dk, ..., d1, d_0 from left to right, the value is:
dk × n^k + ... + d1 × n + d_0 × 1
Each digit is multiplied by the power of the base that matches its position. The rightmost position is n^0 = 1, and the exponent grows by one for each step to the left.
Take 213 in base 5, written 213₅:
| Digit | 2 | 1 | 3 |
|---|---|---|---|
| Place value | 5² = 25 | 5¹ = 5 | 5⁰ = 1 |
| Contribution | 50 | 5 | 3 |
The total is 50 + 5 + 3 = 58, so 213₅ = 58 in decimal.
A second example is 101101₂. The places from the right are 1, 2, 4, 8, 16, 32. The digits 1, 0, 1, 1, 0, 1 are read left to right, so the 1s sit at 32, 8, 4 and 1. The total is 32 + 8 + 4 + 1 = 45.
For binary, you only add the place values wherever a 1 appears. No multiplication is needed.
Letters as digits
A base above 10 needs more than ten symbols. The usual convention is A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. So 2D₁₆ = 2 × 16 + 13 = 45. In a base-12 numeral, 1B7₁₂ = 1 × 144 + 11 × 12 + 7 = 283.
CAT questions rarely go beyond base 16. When they do, the problem statement usually tells you what the symbols stand for.
Horner's method: a faster way to expand
Computing large powers of the base by hand is slow and error-prone. A better approach is to process the digits from left to right:
- Start with a running value of 0.
- For each digit, multiply the running value by the base, then add the digit.
- After the last digit, the running value is the answer.
For 213₅:
| Digit read | Running value before | Calculation | Running value after |
|---|---|---|---|
| 2 | 0 | 0 × 5 + 2 | 2 |
| 1 | 2 | 2 × 5 + 1 | 11 |
| 3 | 11 | 11 × 5 + 3 | 58 |
This gives the same answer as the power-by-power sum. It works because multiplying by the base once for every digit that follows is exactly what produces the power n^i for digit d_i. You never need to write down a power.
The method in code
The next function implements Horner's method. It reads each character as a single digit and builds the value.
def from_base(digits, base):
value = 0
for ch in digits:
value = value * base + int(ch, 16)
return value
print(from_base("101101", 2)) # 45
print(from_base("213", 5)) # 58The call int(ch, 16) turns one character such as "7" or "D" into its numeric value. It accepts letters up to F, so it handles every base up to 16.
This version does not check whether a digit is legal for the base. A safer version rejects illegal digits:
def from_base_checked(digits, base):
value = 0
for ch in digits.upper():
d = int(ch, 36) # accepts 0-9 and A-Z
if d >= base:
raise ValueError(f"digit {ch} is not allowed in base {base}")
value = value * base + d
return value
print(from_base_checked("2D", 16)) # 45
print(from_base_checked("1B7", 12)) # 283
print(int("213", 5)) # 58, Python's built-in agreesPython's built-in int(text, base) does the same job for bases 2 to 36. It is useful for checking your hand work, but you cannot use it in the exam.
Rules every digit must obey
In base n, the only legal digits are 0 to n − 1. This has two consequences you can use immediately:
- A numeral such as 1352 cannot be in base 5, because the digit 5 is not allowed. It could be in base 6 or higher.
- The largest digit in a numeral gives a lower bound on the base. If the largest digit is m, the base is at least m + 1.
The second point matters a great deal for unknown-base problems later in the tutorial, because it limits which values of n you must test.
Two more edge cases:
- Leading zeros do not change the value: 0213₅ = 213₅ = 58.
- A trailing zero multiplies the value by the base. 2130₅ = 58 × 5 = 290. This is the base-n version of how adding a zero to a decimal number multiplies it by 10.
Quick checks for your answer
You can verify a conversion in a few seconds without redoing it.
Size check. A k-digit numeral in base n lies between n^(k−1) and n^k − 1. For a three-digit base-5 numeral, the value must lie between 25 and 124. The answer 58 passes. If you got 140, you made an error.
Parity check. If the base is even, the number is odd exactly when the last digit is odd. Binary and base 16 work this way. If the base is odd, the number is odd exactly when the digit sum is odd, so the digits' parity count matters, not just the last one.
Remainder check with n − 1. In base n, every power of n leaves remainder 1 when divided by n − 1. So a number and its digit sum leave the same remainder on division by n − 1. This is the base-n version of casting out nines. For 213₅, the digit sum is 6, and 6 mod 4 = 2. The decimal value is 58, and 58 mod 4 = 2. The two remainders match, which supports the answer.
Common errors
- Starting the powers at 1 instead of 0. The rightmost digit is multiplied by n^0 = 1, not by n. If you do this, your answer is too large by a factor of n.
- Reading the digits in the wrong order. The leftmost digit has the highest power. Reading right to left reverses the numeral and gives a different number.
- Using a digit that is too large for the base. Check the digits first. If one is not allowed, the question has a different base than you assumed.
- Treating the letter digits as variables. In 2D₁₆, D is the number 13, not an unknown.
- Mixing up the base with the number. 10₅ is five, not ten. The numeral "10" always means one times the base plus zero.
Worked example under exam conditions
Question: the number 3402 is written in base 5. What is its decimal value?
Using Horner's method:
- 0 × 5 + 3 = 3
- 3 × 5 + 4 = 19
- 19 × 5 + 0 = 95
- 95 × 5 + 2 = 477
Check with the size rule: a four-digit base-5 number lies between 125 and 624, and 477 does. Check with the remainder rule: the digit sum is 3 + 4 + 0 + 2 = 9, and 9 mod 4 = 1. Also 477 = 4 × 119 + 1, so 477 mod 4 = 1. The checks agree.
Exercise
Convert 1B7 in base 12 to decimal, using B = 11. Then check your answer with the n − 1 remainder rule.
Show answer
Horner's method gives 0 × 12 + 1 = 1, then 1 × 12 + 11 = 23, then 23 × 12 + 7 = 283. For the check, the base is 12, so use remainders mod 11. The digit sum is 1 + 11 + 7 = 19, and 19 mod 11 = 8. Also 283 = 11 × 25 + 8, so 283 mod 11 = 8. The remainders match, so 283 is consistent.
Exercise
A student claims that 1352 is a number in base 5. Is that possible? What is the smallest base in which 1352 could be written?
Show answer
It is not possible. The digit 5 is not allowed in base 5, whose digits run from 0 to 4. The largest digit in 1352 is 5, so the base must be at least 6. The smallest possible base is 6.
Exercise
Without converting fully, decide whether 4231 in base 6 can equal 1000 in decimal. Use the size rule, then confirm by converting.
Show answer
A four-digit base-6 number lies between 6³ = 216 and 6⁴ − 1 = 1295, so 1000 is not ruled out by size. Now convert: 0 × 6 + 4 = 4, then 4 × 6 + 2 = 26, then 26 × 6 + 3 = 159, then 159 × 6 + 1 = 955. So 4231₆ = 955, not 1000. The size rule alone could not reject the claim, which shows it is a necessary test but not a sufficient one.