Sub topic 1
Positional Number Systems and Base Conversion
Prerequisites
This chapter assumes you already know the following:
- Decimal place value. In 4,072 the digit 4 stands for 4 × 1000.
- Basic binary addition, such as 1 + 1 = 10 in binary.
- Powers of two and the exponent rules, including 2^a × 2^b = 2^(a+b) and 2^0 = 1.
You do not need any prior knowledge of complement arithmetic or floating point. Later chapters build on the conversions you practice here.
The positional model
A positional number system writes a number as a row of digits. Each digit is weighted by a power of the system's base (also called the radix). In base r, the digits are 0 to r − 1, and the digit string d(n-1) ... d1 d_0 has the value:
value = d(n-1) × r^(n-1) + ... + d1 × r + d_0
The same rule extends to the right of the radix point. A digit k places after the point has weight r^(-k).
value = ... + d0 × r^0 + d(-1) × r^(-1) + d_(-2) × r^(-2) + ...
The four bases that matter for GATE are in this table.
| Base | Name | Digits | Typical use |
|---|---|---|---|
| 2 | Binary | 0, 1 | Hardware storage and logic |
| 8 | Octal | 0 to 7 | Compact binary (3 bits per digit) |
| 10 | Decimal | 0 to 9 | Human input and output |
| 16 | Hexadecimal | 0 to 9, A to F | Compact binary (4 bits per digit) |
In hexadecimal, A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.
Here is a worked evaluation of 1101.01 in base 2:
- 1 × 8 + 1 × 4 + 0 × 2 + 1 × 1 = 13
- 0 × 1/2 + 1 × 1/4 = 0.25
- Total: 13.25
The digit 7 is invalid in base 7 and the digit 8 is invalid in octal. A question that gives such a digit has no valid answer in that base, so check digits before you convert.
Converting any base to decimal
Use the positional formula directly. For integers, Horner's method needs fewer multiplications. You start with 0 and, for each digit from left to right, compute acc = acc × r + digit.
For example, 2F3 in hexadecimal:
- acc = 0 × 16 + 2 = 2
- acc = 2 × 16 + 15 = 47
- acc = 47 × 16 + 3 = 755
So 2F3 (base 16) = 755 (base 10).
Converting decimal to another base
Integer and fractional parts need different procedures. Split the number at the radix point and convert each part separately.
Integer part: repeated division
Divide by the target base r repeatedly. Record each remainder. Stop when the quotient is 0. The remainders, read from last to first, are the digits.
Convert 156 to binary:
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 156 / 2 | 78 | 0 |
| 2 | 78 / 2 | 39 | 0 |
| 3 | 39 / 2 | 19 | 1 |
| 4 | 19 / 2 | 9 | 1 |
| 5 | 9 / 2 | 4 | 1 |
| 6 | 4 / 2 | 2 | 0 |
| 7 | 2 / 2 | 1 | 0 |
| 8 | 1 / 2 | 0 | 1 |
Reading the remainders from bottom to top gives 10011100. Check: 128 + 16 + 8 + 4 = 156.
Fractional part: repeated multiplication
Multiply the fraction by r. The integer part of the product is the next digit. Keep the fractional part and repeat. Stop when the fraction becomes 0 or you have enough digits. Here the digits are read from first to last.
Convert 0.8125 to binary:
| Step | Multiplication | Integer part (digit) | Fraction left |
|---|---|---|---|
| 1 | 0.8125 × 2 = 1.625 | 1 | 0.625 |
| 2 | 0.625 × 2 = 1.25 | 1 | 0.25 |
| 3 | 0.25 × 2 = 0.5 | 0 | 0.5 |
| 4 | 0.5 × 2 = 1.0 | 1 | 0 |
So 0.8125 = 0.1101 in binary. The fraction reached 0, so the expansion is exact.
Combining both parts, 156.8125 (base 10) = 10011100.1101 (base 2).
Fractions that never terminate
A fraction has a finite binary expansion only if, in lowest terms, its denominator is a power of 2. Any other denominator gives a repeating pattern. Convert 0.1:
- 0.1 × 2 = 0.2, digit 0
- 0.2 × 2 = 0.4, digit 0
- 0.4 × 2 = 0.8, digit 0
- 0.8 × 2 = 1.6, digit 1, fraction 0.6
- 0.6 × 2 = 1.2, digit 1, fraction 0.2
- The fraction 0.2 has appeared before, so the cycle repeats.
The result is 0.0001100110011... with the block 0011 repeating forever. This is the root cause of the floating-point surprises covered in a later chapter.
Shortcuts between binary, octal and hexadecimal
Because 8 = 2^3 and 16 = 2^4, each octal digit maps to exactly 3 bits and each hex digit maps to exactly 4 bits. No arithmetic is needed. Group the bits and look up each group.
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
The grouping rule has one edge case that is easy to get wrong. Start grouping at the radix point and move outward. Pad the integer side with leading zeros on the left, and pad the fractional side with trailing zeros on the right.
Convert 1011110.10111 to hexadecimal:
- Integer part: 1011110 becomes 0101 1110, which is 5E.
- Fractional part: 10111 becomes 1011 1000, which is B8.
- Result: 5E.B8 (base 16).
Padding the fractional side on the left instead would give 1011 and 0111, which is wrong. Trailing zeros on the fraction do not change its value, so they go on the right.
For octal, use groups of 3. For example, 1011110.10111 becomes 001 011 110 . 101 110, which is 136.56 (base 8).
To convert between octal and hexadecimal, go through binary. Regroup the bits from 3-bit groups to 4-bit groups, or the reverse.
Verifying conversions in Python
Python can check your hand results. The functions below convert an integer to any base from 2 to 16 and a fraction with a limit on the number of digits.
DIGITS = "0123456789ABCDEF"
def int_to_base(n, base):
if n == 0:
return "0"
out = []
while n > 0:
n, r = divmod(n, base)
out.append(DIGITS[r])
return "".join(reversed(out))
def frac_to_base(f, base, max_digits=12):
out = []
while f > 0 and len(out) < max_digits:
f *= base
d = int(f)
out.append(DIGITS[d])
f -= d
return "".join(out)
print(int_to_base(156, 2)) # 10011100
print(frac_to_base(0.8125, 2)) # 1101
print(int_to_base(755, 16)) # 2F3
print(int(" 2F3".strip(), 16)) # 755
print(frac_to_base(0.1, 2, 12)) # 000110011001 (approximate, since 0.1 is stored as a double)Python's built-in int(text, base) parses a string in any base from 2 to 36. The functions bin, oct and hex format an integer in bases 2, 8 and 16.
Common errors
- Reading remainders top to bottom. The first remainder is the least significant digit, so the list must be reversed.
- Dropping the integer part's leading zeros incorrectly. Leading zeros do not change the value, but they matter when a question fixes a bit width, such as "write 5 in 8 bits".
- Grouping from the wrong end. Integer bits group from the point leftward. Fraction bits group from the point rightward.
- Treating a repeating fraction as exact. 0.1 in decimal cannot be written exactly with a finite number of bits, so you must state how many bits you keep.
- Using the wrong weight for fractional digits. The first digit after the point has weight r^(-1), not r^0.
- Confusing digit count and value. A 4-digit hexadecimal number has 16 bits, not 4.
Counting digits and values
Two quick facts help with GATE questions on size:
- n digits in base r can represent r^n distinct values, ranging from 0 to r^n − 1 when unsigned.
- Representing a value N in base r needs ⌈logr(N + 1)⌉ digits. For example, 1000 needs ⌈log2(1001)⌉ = 10 bits, because 2^10 = 1024.
Check with the bit-width of the largest value: 2^10 − 1 = 1023 ≥ 1000, while 2^9 − 1 = 511 < 1000.
Exercise
Convert 45.375 (base 10) to binary, then write the result in hexadecimal.
Show answer
Integer part: 45 = 32 + 8 + 4 + 1 = 101101. Fraction: 0.375 × 2 = 0.75 (digit 0), 0.75 × 2 = 1.5 (digit 1), 0.5 × 2 = 1.0 (digit 1), so 0.375 = 0.011. Binary: 101101.011. Group in 4s from the point: integer 0010 1101 = 2D, fraction 0110 = 6. Hexadecimal: 2D.6.
Exercise
How many bits are needed to store the decimal number 5000 as an unsigned integer, and what is its hexadecimal form?
Show answer
2^12 = 4096 is less than 5000 and 2^13 = 8192 is greater, so 13 bits are needed. Hexadecimal: 5000 / 16 = 312 remainder 8, 312 / 16 = 19 remainder 8, 19 / 16 = 1 remainder 3, 1 / 16 = 0 remainder 1. Reading the remainders from last to first gives 1388 (base 16).
Exercise
Why does the fraction 0.3 (base 10) have no finite binary expansion, while 0.375 does?
Show answer
A fraction terminates in binary only if its denominator in lowest terms is a power of 2. 0.375 = 3/8 and 8 = 2^3, so it terminates. 0.3 = 3/10 and 10 has the factor 5, so the digits repeat forever.