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) uses the digits (for the letters A, B, C, ... stand for 10, 11, 12, ...). The digit string has the value
The position of a digit gives its weight, a power of . The point separates the non-negative powers from the negative ones.
The four bases you must know.
| Base | Name | Digits | Bits per digit |
|---|---|---|---|
| 2 | binary | 0 1 | 1 |
| 8 | octal | 0 to 7 | 3 |
| 10 | decimal | 0 to 9 | not a whole number of bits |
| 16 | hexadecimal | 0 to 9, A to F | 4 |
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 (repeated division). Divide the number by , 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 (repeated multiplication). Multiply the fraction by . 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 and , 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 in lowest terms has a finite expansion in base if and only if every prime factor of is also a prime factor of . Otherwise the digits repeat forever. For base 2 that means must be a power of 2. So , and are finite in binary, while , and are not. In base 10, may only have the factors 2 and 5.
How many digits does a number need? The positive integer has digits in base . To distinguish different values with bits you need bits. In particular bits give patterns, the largest unsigned value is , and an -digit string in base gives values.
Arithmetic in a base. The school rules apply in any base: add digit by digit, carry when the column sum reaches . In binary the column rules are , , (write 0, carry 1) and (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 typedWorking
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)
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.
Three habits make conversions fast and safe:
- Memorise the powers of two from to (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.
- Group, do not divide, when both bases are powers of two.
- 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 10011100Step 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). .
Step 3 (group by fours). 1001 is 9 and 1100 is C, so the hexadecimal form is 9C. Check: .
Step 4 (group by threes from the right). Pad to 010 011 100, which is 2, 3, 4. The octal form is 234. Check: .
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). gives digit 1, keep 0.375. Then gives 0, keep 0.75; gives 1, keep 0.5; gives 1, keep 0. The fraction ended, so . Check: .
Step 2 (why it ended). and , so only the factor 2 appears in the denominator.
Step 3 (0.1). and . The factor 5 is not a factor of 2, so the expansion never ends: after the first digit 0 the block 0011 repeats forever, .
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
MediumAdd .
Step 1 (align the points and pad).
1011.101
+ 110.110Step 2 (add column by column from the right). Fraction columns: , , (write 0, carry 1), so the fraction of the sum is and a carry of 1 goes into the integer part. Integer columns: (write 0, carry 1); (write 1, carry 1); (write 0, carry 1); (write 0, carry 1); the last carry becomes the leading 1. Reading the digits from the left gives .
Step 3 (check in decimal). and . Their sum is , and .
EX 4Find the base from an equation
GATE levelIn which base does have the roots 3 and 5? (All numbers in the equation are written in that base.)
Step 1. If the base is , then "10" means and "17" means . So the equation is .
Step 2. The sum of the roots is , and it equals . So .
Step 3 (check with the product). The product of the roots is , and it must equal . It does.
Step 4 (check the digits). The digit 7 must be less than the base, and . 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 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 , both equal to 2035 in decimal.
(b) Bits for a six-digit decimal number. The largest value is . Since , 20 bits are needed (the program counts 20 significant bits of 999999).
(c) Hexadecimal to decimal. .
EX 6An equation that holds in every sufficiently large base
GATE levelIs true, and in which bases?
Step 1. and , so the product is .
Step 2. . 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 . There is no single answer, and GATE questions that look like this usually add a second equation to pin down .
Cheat box
Cheat box
- Value of a digit string: (negative after the point).
- Integer: divide by , read remainders last to first. Fraction: multiply by , read integer parts first to last.
- Octal digit = 3 bits, hexadecimal digit = 4 bits. Group from the point outwards.
- (lowest terms) ends in base iff every prime factor of divides . In binary: iff is a power of 2.
- Digits of in base : . Bits for values: . bits: to .
- 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 in binary: it never ends.
- Largest digit in base is . "10" in base always means .
- 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 digits is (leading zeros count), while the number of -digit numbers without a leading zero is .
Connections
Builds on
No prerequisites.
Unlocks
Across subjects
Within this subject
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
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
A particular number is written as 132 in radix-4 representation. The same number in radix-5 representation is __________.
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
If x and y are two decimal digits and (0.1101)2=(0.8xy5)10, the decimal value of x+y is?
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
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 ______.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
Consider the equation (43)x = (y3)8 where x and y are unknown. The number of possible solutions is ________
Consider the equation (123)5 = (x8)y with x and y as unknown. The number of possible solutions is ______.
The base (or radix) of the number system such that the following equation holds 312 20 = 13.1 is ____________ DIGITAL LOGIC
(1217)8 is equivalent to
- (a)(1217)16
- (b)(028F)16
- (c)(2297)10
- (d)(0B17)16
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
The hexadecimal representation of (657)8 is:
- (a)1AF
- (b)D78
- (c)D71
- (d)32F
Self-check
Question 1
Write in binary.
Show answerHide
gives 0; gives 1; gives 0; gives 1. The result is . Check: .
Question 2
Convert to hexadecimal.
Show answerHide
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 . Check: , and .
Question 3
Does have a finite binary expansion? Does ?
Show answerHide
: the denominator has the prime factor 3, which does not divide 2, so it repeats. : , and 5 does not divide 2, so it repeats too. (In base 10, ends, because 2 and 5 divide 10.)
Question 4
How many hexadecimal digits are needed to write any 20-bit unsigned number?
Show answerHide
Each hexadecimal digit carries 4 bits, so digits.
Revision card
One-screen revision
- Value . Letters A to F are 10 to 15.
- Integer to base : divide, read remainders last to first. Fraction to base : 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.
- bits: values, maximum . Bits for values: .
- Unknown base: write each number as a polynomial in , solve, check that all digits are less than .
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)