G27
Self-checkedTier 1 14 min readdl.number-systems-conversion

Number systems and conversion

Positional notation in any base, converting between decimal, binary, octal and hexadecimal (integers and fractions), when a fraction stops, and how many digits or bits a number needs.

Why it exists

A digital circuit can only tell two voltage levels apart, so every number inside a computer is written with the two digits 0 and 1. People and programs, however, write numbers in decimal, and hardware designers and debuggers write them in octal or hexadecimal because those are short ways of writing long bit strings. GATE questions in this area are almost all conversions: write this number in another base, find the unknown base from an equation, or say how many bits or digits are needed. All of them follow from one idea, positional notation, and a handful of mechanical procedures.

Everything later in this subject (signed numbers, floating point, adders, memory addressing in COA) assumes that you can move between these bases without thinking.

Prerequisites

None. This is a starting point.

Nothing from earlier pages. You need to be comfortable with exponents and with integer division with remainder.

Precise definitions

Positional number system. A base (radix) b≥2b \ge 2 uses the digits 0,1,…,b−10, 1, \dots, b-1 (for b>10b > 10 the letters A, B, C, ... stand for 10, 11, 12, ...). The digit string dk dk−1…d1 d0 . d−1 d−2…d−md_{k}\,d_{k-1}\dots d_1\,d_0\,.\,d_{-1}\,d_{-2}\dots d_{-m} has the value

∑i=−mkdi b i.\sum_{i=-m}^{k} d_i\, b^{\,i}.

The position of a digit gives its weight, a power of bb. The point separates the non-negative powers from the negative ones.

The four bases you must know.

BaseNameDigitsBits per digit
2binary0 11
8octal0 to 73
10decimal0 to 9not a whole number of bits
16hexadecimal0 to 9, A to F4

Bit, nibble, byte. A bit is one binary digit. A nibble is 4 bits (one hexadecimal digit). A byte is 8 bits (two hexadecimal digits).

Conversion of the integer part from decimal to base bb (repeated division). Divide the number by bb, record the remainder, replace the number by the quotient, and repeat until the quotient is 0. The remainders, read from the last to the first, are the digits from the most significant to the least significant.

Conversion of the fractional part from decimal to base bb (repeated multiplication). Multiply the fraction by bb. The integer part of the product is the next digit after the point (the first digit produced is the most significant). Keep the fractional part and repeat, until the fractional part is 0 or you have the number of digits you want. Read the digits in the order they are produced.

Conversion to decimal. Add digit times weight for every position (this is the formula above).

Binary, octal and hexadecimal are interchangeable by grouping. Since 8=238 = 2^3 and 16=2416 = 2^4, one octal digit is exactly three bits and one hexadecimal digit is exactly four. Group the bits in threes (octal) or fours (hexadecimal) starting at the binary point and moving outwards, padding the outer ends with zeros: the integer part is padded on the left, the fractional part on the right. Then translate each group. Going the other way, replace each digit by its 3-bit or 4-bit group. No arithmetic is needed, and octal to hexadecimal goes through binary.

When does a fraction stop? A fraction p/qp/q in lowest terms has a finite expansion in base bb if and only if every prime factor of qq is also a prime factor of bb. Otherwise the digits repeat forever. For base 2 that means qq must be a power of 2. So 0.50.5, 0.375=3/80.375 = 3/8 and 0.6875=11/160.6875 = 11/16 are finite in binary, while 0.1=1/100.1 = 1/10, 0.20.2 and 1/31/3 are not. In base 10, qq may only have the factors 2 and 5.

How many digits does a number need? The positive integer nn has ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1 digits in base bb. To distinguish NN different values with bits you need ⌈log⁡2N⌉\lceil \log_2 N \rceil bits. In particular nn bits give 2n2^n patterns, the largest unsigned value is 2n−12^n - 1, and an nn-digit string in base bb gives bnb^n values.

Arithmetic in a base. The school rules apply in any base: add digit by digit, carry when the column sum reaches bb. In binary the column rules are 0+0=00+0=0, 0+1=10+1=1, 1+1=101+1=10 (write 0, carry 1) and 1+1+1=111+1+1=11 (write 1, carry 1).

How it works

The animation walks through the five procedures on real numbers. Every line is computed by exact integer and fraction arithmetic by a script (not typed by hand), and the tests decode each row again independently. Use the step button and read the remainders or the doubled values as they appear.

Number system conversions: Decimal to binary: repeated division

Computed exactly, not typed

Working

156 / 2 = 78 remainder 0
78 / 2 = 39 remainder 0
39 / 2 = 19 remainder 1
19 / 2 = 9 remainder 1
9 / 2 = 4 remainder 1
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1

Value, format and bits (changed bits in amber)

decimalbase 10= 156
remainders, last to firstbase 2= 156
10011100

Step 1

Divide by 2 until the quotient is 0. Each remainder is one binary digit, the first remainder being the least significant bit. Reading the remainders from the last to the first gives the number. Check by adding the place values: 128 + 16 + 8 + 4 = 156.

1 / 5
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. Python 3.15.1, darwin arm64. python3 scripts/gen-dl-traces.py

Three habits make conversions fast and safe:

  1. Memorise the powers of two from 202^0 to 2162^{16} (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536). Most decimal to binary conversions are then a subtraction game: write the largest power that fits, subtract, continue.
  2. Group, do not divide, when both bases are powers of two.
  3. Check by converting back, or at least by a parity test: a binary integer is odd exactly when its last bit is 1, and a hexadecimal integer is odd exactly when its last digit is odd.

Worked examples

EX 1Decimal 156 in binary, octal and hexadecimal

Easy
#include <stdio.h>
 
int main(void) {
    unsigned n = 156;
    printf("octal %o hex %X\n", n, n);
    printf("binary ");
    for (int i = 7; i >= 0; i--) putchar(((n >> i) & 1) ? '1' : '0');
    printf("\n");
    return 0;
}

Output:

octal 234 hex 9C
binary 10011100

Step 1 (repeated division by 2). The remainders, first to last, are 0, 0, 1, 1, 1, 0, 0, 1. Read from last to first they give 10011100.

Step 2 (check with place values). 128+16+8+4=156128 + 16 + 8 + 4 = 156.

Step 3 (group by fours). 1001 is 9 and 1100 is C, so the hexadecimal form is 9C. Check: 9⋅16+12=1569 \cdot 16 + 12 = 156.

Step 4 (group by threes from the right). Pad to 010 011 100, which is 2, 3, 4. The octal form is 234. Check: 2⋅64+3⋅8+4=1562 \cdot 64 + 3 \cdot 8 + 4 = 156.

EX 2Fractions: one that ends and one that does not

Medium
#include <stdio.h>
 
int main(void) {
    double x = 0.6875;
    printf("0.");
    while (x != 0.0) {
        x *= 2;
        if (x >= 1.0) { putchar('1'); x -= 1.0; } else putchar('0');
    }
    printf("\n");
    double y = 0.1;
    printf("0.");
    for (int i = 0; i < 24; i++) {
        y *= 2;
        if (y >= 1.0) { putchar('1'); y -= 1.0; } else putchar('0');
    }
    printf("...\n");
    return 0;
}

Output:

0.1011
0.000110011001100110011001...

Step 1 (0.6875). 0.6875×2=1.3750.6875 \times 2 = 1.375 gives digit 1, keep 0.375. Then 0.750.75 gives 0, keep 0.75; 1.51.5 gives 1, keep 0.5; 1.01.0 gives 1, keep 0. The fraction ended, so 0.6875=(0.1011)20.6875 = (0.1011)_2. Check: 1/2+1/8+1/16=11/161/2 + 1/8 + 1/16 = 11/16.

Step 2 (why it ended). 0.6875=11/160.6875 = 11/16 and 16=2416 = 2^4, so only the factor 2 appears in the denominator.

Step 3 (0.1). 0.1=1/100.1 = 1/10 and 10=2⋅510 = 2 \cdot 5. The factor 5 is not a factor of 2, so the expansion never ends: after the first digit 0 the block 0011 repeats forever, 0.1=(0.00011‾)20.1 = (0.0\overline{0011})_2.

Lesson. The program doubles an approximation of 0.1 stored as a double. Doubling a double is exact, and the first 24 digits agree with the true expansion; but the last stored digits of the double differ from the infinite pattern, which is why decimal fractions are rounded in floating point (see IEEE 754).

EX 3Binary arithmetic with a fraction

Medium

Add (1011.101)2+(110.11)2(1011.101)_2 + (110.11)_2.

Step 1 (align the points and pad).

  1011.101
+  110.110

Step 2 (add column by column from the right). Fraction columns: 1+0=11+0=1, 0+1=10+1=1, 1+1=101+1=10 (write 0, carry 1), so the fraction of the sum is .011.011 and a carry of 1 goes into the integer part. Integer columns: 1+0+1=101+0+1=10 (write 0, carry 1); 1+1+1=111+1+1=11 (write 1, carry 1); 0+1+1=100+1+1=10 (write 0, carry 1); 1+0+1=101+0+1=10 (write 0, carry 1); the last carry becomes the leading 1. Reading the digits from the left gives 10010.01110010.011.

Step 3 (check in decimal). (1011.101)2=11+5/8=11.625(1011.101)_2 = 11 + 5/8 = 11.625 and (110.11)2=6+3/4=6.75(110.11)_2 = 6 + 3/4 = 6.75. Their sum is 18.37518.375, and (10010.011)2=16+2+1/4+1/8=18.375(10010.011)_2 = 16 + 2 + 1/4 + 1/8 = 18.375.

EX 4Find the base from an equation

GATE level

In which base does x2−10x+17=0x^2 - 10x + 17 = 0 have the roots 3 and 5? (All numbers in the equation are written in that base.)

Step 1. If the base is bb, then "10" means bb and "17" means b+7b + 7. So the equation is x2−bx+(b+7)=0x^2 - bx + (b+7) = 0.

Step 2. The sum of the roots is 3+5=83 + 5 = 8, and it equals bb. So b=8b = 8.

Step 3 (check with the product). The product of the roots is 1515, and it must equal b+7=15b + 7 = 15. It does.

Step 4 (check the digits). The digit 7 must be less than the base, and 7<87 < 8. The answer is base 8.

EX 5Octal and hexadecimal through binary, and counting bits

GATE level
#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    printf("%ld %ld %ld\n", strtol("2A3", NULL, 16), strtol("3763", NULL, 8), strtol("10011100", NULL, 2));
    printf("%X %o\n", 0x7F3, 0x7F3);
    int bits = 0;
    unsigned long long v = 999999ULL;
    while (v) { bits++; v >>= 1; }
    printf("%d\n", bits);
    return 0;
}

Output:

675 2035 156
7F3 3763
20

(a) Hexadecimal 7F37F3 to octal. Write each digit as four bits: 0111 1111 0011. Regroup the same twelve bits in threes: 011 111 110 011, which is 3, 7, 6, 3. So (7F3)16=(3763)8(7F3)_{16} = (3763)_8, both equal to 2035 in decimal.

(b) Bits for a six-digit decimal number. The largest value is 999999999999. Since 219=524288<106≤220=10485762^{19} = 524288 < 10^6 \le 2^{20} = 1048576, 20 bits are needed (the program counts 20 significant bits of 999999).

(c) Hexadecimal to decimal. (2A3)16=2⋅256+10⋅16+3=675(2A3)_{16} = 2 \cdot 256 + 10 \cdot 16 + 3 = 675.

EX 6An equation that holds in every sufficiently large base

GATE level

Is (12)b×(13)b=(156)b(12)_b \times (13)_b = (156)_b true, and in which bases?

Step 1. (12)b=b+2(12)_b = b + 2 and (13)b=b+3(13)_b = b + 3, so the product is b2+5b+6b^2 + 5b + 6.

Step 2. (156)b=b2+5b+6(156)_b = b^2 + 5b + 6. The two sides are identical polynomials.

Step 3 (which bases are legal). The digit 6 must be less than the base, so the equation holds in every base b≥7b \ge 7. There is no single answer, and GATE questions that look like this usually add a second equation to pin down bb.

Cheat box

Cheat box

  • Value of a digit string: ∑dibi\sum d_i b^i (negative ii after the point).
  • Integer: divide by bb, read remainders last to first. Fraction: multiply by bb, read integer parts first to last.
  • Octal digit = 3 bits, hexadecimal digit = 4 bits. Group from the point outwards.
  • p/qp/q (lowest terms) ends in base bb iff every prime factor of qq divides bb. In binary: iff qq is a power of 2.
  • Digits of nn in base bb: ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1. Bits for NN values: ⌈log⁡2N⌉\lceil \log_2 N \rceil. nn bits: 00 to 2n−12^n - 1.
  • Powers of two: 1 2 4 8 16 32 64 128 256 512 1024 (2^10 = 1 Ki), 2^16 = 65536, 2^20 = 1 Mi.
  • 0.1 decimal is 0.00011‾0.0\overline{0011} in binary: it never ends.
  • Largest digit in base bb is b−1b - 1. "10" in base bb always means bb.
  • Parity: a number is odd in binary iff its last bit is 1.

Traps and GATE patterns

Other things examiners twist:

  • Questions that mix a fraction and an integer part in one conversion. Convert the two parts separately and join them at the point.
  • Subtraction or addition performed directly in octal or hexadecimal. Carry when the column reaches the base, not 10.
  • The number of distinct values in a string of nn digits is bnb^n (leading zeros count), while the number of nn-digit numbers without a leading zero is bn−bn−1b^n - b^{n-1}.

Connections

Within this subject

Sign-magnitude, 1's and 2's complementusesIEEE 754 floating pointuses
BCD, Gray and parity codescontrasts-withplanned

Fixed-length strings of bits are interpreted as negative numbers in signed number representation and as numbers with a fractional part in IEEE 754. The same ideas explain the output of printf("%o %X") and the widths of types in C types and operators. Binary codes that are not positional, such as BCD and Gray code, are treated in codes.

PYQs

13 previous year questions from your book. I re-solved each one myself (13 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2024 · Set 2 · MSQ · 2 marksQ7 · page 154 of the book
Which of the following is/are EQUAL to 224 in
radix-5 (i.e., base-5) notation?
  • (a)64 in radix-10
  • (b)100 in radix-8
  • (c)50 in radix-16
  • (d)121 in radix-7
GATE 2023 · NAT · 1 markQ9 · page 154 of the book
A particular number is written as 132 in radix-4
representation. The same number in radix-5
representation is __________.
GATE 2021 · Set 2 · MSQQ12 · page 154 of the book
If the numerical value of a 2-byte unsigned integer on
a little-endian computer is 255 more than that on a
big-endian computer, which of the following choices
represent(s) the unsigned integer on a little-endian
computer?
  • (a)0x6665
  • (b)0x0001
  • (c)0x4243
  • (d)0x0100
GATE 2021 · Set 2 · NAT · 1 markQ13 · page 154 of the book
If x and y are two decimal digits and

(0.1101)2=(0.8xy5)10, the decimal value of x+y is?
GATE 2021 · Set 1 · MCQ · 1 markQ14 · page 154 of the book
Let the representation of a number in base 3 be 210.
What is the hexadecimal representation of the
number?
  • (a)15
  • (b)21
  • (c)528
  • (d)D2
GATE 2019 · NAT · 1 markQ17 · page 155 of the book
Two numbers are chosen independently and
uniformly at random from the set {1, 2, ..., 13}. The
probability (rounded off to 3 decimal places) that
their 4-bit (unsigned) binary representations have
the same most significant bit is ______.
GATE 2017 · Set 2 · MCQ · 1 markQ19 · page 155 of the book
The representation of the value of a 16-bit unsigned
integer X in the hexadecimal number system is
BCA9. The representation of the value of X in octal
number system is
  • (a)571244
  • (b)736251
  • (c)571247
  • (d)136251
GATE 2015 · Set 3 · NAT · 1 markQ22 · page 155 of the book
Consider the equation (43)x = (y3)8
where x and y are unknown. The number of possible
solutions is ________
GATE 2014 · Set 2 · NAT · 1 markQ23 · page 155 of the book
Consider the equation (123)5 = (x8)y with x and y
as unknown. The number of possible solutions is
______.
GATE 2014 · Set 1 · NAT · 1 markQ24 · page 155 of the book
The base (or radix) of the number system such that
the following equation holds

312
20 = 13.1 is ____________

DIGITAL LOGIC
GATE 2009 · MCQ · 1 markQ27 · page 156 of the book
(1217)8 is equivalent to
  • (a)(1217)16
  • (b)(028F)16
  • (c)(2297)10
  • (d)(0B17)16
GATE 2008 · MCQQ29 · page 156 of the book
Let r denote the number system radix. The only
value(s) of r that satisfy the equation

√121r = 11r is/are
  • (a)decimal 10
  • (b)decimal 11
  • (c)decimal 10 and 11
  • (d)any value > 2
GATE 2005 · MCQ · 1 markQ35 · page 157 of the book
The hexadecimal representation of (657)8 is:
  • (a)1AF
  • (b)D78
  • (c)D71
  • (d)32F

Self-check

Question 1

Write (0.3125)10(0.3125)_{10} in binary.

Show answer

0.3125×2=0.6250.3125 \times 2 = 0.625 gives 0; 0.625×2=1.250.625 \times 2 = 1.25 gives 1; 0.25×2=0.50.25 \times 2 = 0.5 gives 0; 0.5×2=1.00.5 \times 2 = 1.0 gives 1. The result is (0.0101)2(0.0101)_2. Check: 1/4+1/16=5/16=0.31251/4 + 1/16 = 5/16 = 0.3125.

Question 2

Convert (1101011.01)2(1101011.01)_2 to hexadecimal.

Show answer

Integer part, grouped from the right: 110 1011 padded to 0110 1011, giving 6B. Fraction part padded on the right: 01 becomes 0100, giving 4. The answer is (6B.4)16(6B.4)_{16}. Check: 6⋅16+11+4/16=107.256 \cdot 16 + 11 + 4/16 = 107.25, and 64+32+8+2+1+1/4=107.2564 + 32 + 8 + 2 + 1 + 1/4 = 107.25.

Question 3

Does 1/61/6 have a finite binary expansion? Does 3/403/40?

Show answer

1/61/6: the denominator has the prime factor 3, which does not divide 2, so it repeats. 3/403/40: 40=23⋅540 = 2^3 \cdot 5, and 5 does not divide 2, so it repeats too. (In base 10, 3/40=0.0753/40 = 0.075 ends, because 2 and 5 divide 10.)

Question 4

How many hexadecimal digits are needed to write any 20-bit unsigned number?

Show answer

Each hexadecimal digit carries 4 bits, so ⌈20/4⌉=5\lceil 20/4 \rceil = 5 digits.

Revision card

One-screen revision

  • Value =∑dibi= \sum d_i b^i. Letters A to F are 10 to 15.
  • Integer to base bb: divide, read remainders last to first. Fraction to base bb: multiply, read digits in order.
  • Octal = groups of 3 bits, hexadecimal = groups of 4, from the point outwards.
  • Finite expansion iff every prime factor of the denominator divides the base. 0.1 is not finite in binary.
  • nn bits: 2n2^n values, maximum 2n−12^n - 1. Bits for NN values: ⌈log⁡2N⌉\lceil \log_2 N \rceil.
  • Unknown base: write each number as a polynomial in bb, solve, check that all digits are less than bb.

References

  • ·Mano and Ciletti, Digital Design, 5th ed., Chapter 1 Digital Systems and Binary Numbers (binary numbers, number-base conversion, octal and hexadecimal)
  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 6 Arithmetic (positional number systems)

Verification

Every conversion in the animation is computed by scripts/gen-dl-traces.py with exact integer and Fraction arithmetic and decoded again by tests/dl-traces.test.ts. The termination rule for fractions, the digit-count formula, the grouping shortcuts and the worked examples are brute-force checked by scripts/enum-checks/dl_numbers.py. All C programs are compiled and run by npm run verify:c. Textbook chapter titles are from memory and have not been checked against the physical books; the independent second pass has not been run yet, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never

My notes (saved in this browser)