Sub topic 1
Positional Number Systems and Base Conversion
Prerequisites
You should know decimal and binary place values, basic binary addition, and powers of two. This chapter assumes that you can read a number such as 1011 in base 2.
The positional model
A positional number system assigns a value to each digit according to its position. In base b, the position immediately left of the radix point has weight b⁰, the next position left has weight b¹, and so on. Positions to the right have weights b⁻¹, b⁻², and so on.
For example:
1011.01₂ = 1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰ + 0 × 2⁻¹ + 1 × 2⁻² = 8 + 2 + 1 + 0.25 = 11.25₁₀
The radix point is called the decimal point only when the base is 10. In general, radix point is the safer term.
The base determines the available digits. Base 2 uses 0 and 1. Base 8 uses 0 through 7. Base 16 uses 0 through 9 and A through F, where A represents 10 and F represents 15.
| Base | Common name | Digits |
|---|---|---|
| 2 | binary | 0–1 |
| 8 | octal | 0–7 |
| 16 | hexadecimal | 0–9, A–F |
Converting an integer to another base
To convert a positive decimal integer, repeatedly divide it by the target base. Record each remainder. Read the remainders from bottom to top.
For 45₁₀ in base 2:
- 45 ÷ 2 gives remainder 1
- 22 ÷ 2 gives remainder 0
- 11 ÷ 2 gives remainder 1
- 5 ÷ 2 gives remainder 1
- 2 ÷ 2 gives remainder 0
- 1 ÷ 2 gives remainder 1
Therefore, 45₁₀ = 101101₂.
To convert an integer from any base to decimal, expand its positional expression. To convert between binary and hexadecimal, group binary digits in sets of four from the radix point outward. Each group maps to one hexadecimal digit. For octal, group digits in sets of three.
def decimal_to_base(number, base):
if number == 0:
return "0"
digits = "0123456789ABCDEF"
result = []
while number:
number, remainder = divmod(number, base)
result.append(digits[remainder])
return "".join(reversed(result))
print(decimal_to_base(45, 2)) # 101101
print(decimal_to_base(255, 16)) # FFConverting fractions
For a decimal fraction, repeatedly multiply by the target base. Record the integer part at each step, then continue with the remaining fraction.
For 0.625₁₀ in binary:
- 0.625 × 2 = 1.25, record 1
- 0.25 × 2 = 0.5, record 0
- 0.5 × 2 = 1.0, record 1
Thus, 0.625₁₀ = 0.101₂.
Some fractions never terminate in a chosen base. For example, 0.1₁₀ has a repeating binary expansion because 1/10 is not expressible using a finite sum of negative powers of 2.
A conversion workflow
GATE shortcut
For binary values, hexadecimal grouping is usually faster than decimal expansion. Pad only at the outside edge of a group, not between meaningful digits. For example:
11010110₂ = 1101 0110₂ = D6₁₆
Exercise
Convert 73₁₀ to hexadecimal and verify the result in decimal.
Show answer
73 ÷ 16 gives quotient 4 and remainder 9, so 73₁₀ = 49₁₆. Verification: 4 × 16 + 9 = 73.