CODEBHAVYA โ€ข MATHEMATICS FOR PROGRAMMING

๐Ÿ”„ Base Conversions

Understand binary, octal, decimal and hexadecimal number systems, then convert values between them using reliable step-by-step methods.

๐ŸŽฏ Learning Objectives

After completing this topic, you should be able to:

  • Identify the base and valid digits of a positional number system.
  • Convert decimal integers and fractions to another base.
  • Convert binary, octal and hexadecimal values to decimal.
  • Use bit grouping for fast binaryโ€“octal and binaryโ€“hexadecimal conversions.
  • Verify conversions and avoid common digit and grouping mistakes.
  • Connect number bases with data representation in programs and computers.

๐Ÿ“– 1. Positional Number Systems

A positional number system gives each digit a value based on both the digit itself and its position. The base tells us how many distinct digits are available and what power is assigned to each position.

Value = dnbn + dn-1bn-1 + ... + d1b1 + d0b0
Example: (352)10 3 ร— 102 + 5 ร— 101 + 2 ร— 100 = 300 + 50 + 2
Important: The rightmost integer digit always has position 0. Positions increase towards the left and become negative after the radix point.

๐Ÿงฉ 2. Common Bases Used in Programming

System Base Valid Digits Example Programming Use
Binary 2 0, 1 (101101)2 Bits, logic and machine data
Octal 8 0โ€“7 (735)8 Compact groups of three bits
Decimal 10 0โ€“9 (245)10 Everyday calculations
Hexadecimal 16 0โ€“9, Aโ€“F (9AF)16 Memory, colours and bit patterns
Hexadecimal digit values A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.

โž— 3. Decimal Integer to Another Base

Repeatedly divide the decimal number by the required base. Record each remainder and read the remainders from bottom to top.

  1. Divide the number by the target base.
  2. Write the remainder.
  3. Use the quotient as the next number.
  4. Repeat until the quotient becomes 0.
  5. Read the remainders in reverse order.

Convert (45)10 to binary

Target base = 2

Repeated division

45 รท 2 = 22, remainder 1

22 รท 2 = 11, remainder 0

11 รท 2 = 5, remainder 1

5 รท 2 = 2, remainder 1

2 รท 2 = 1, remainder 0

1 รท 2 = 0, remainder 1

Reading upwards: (45)10 = (101101)2

๐Ÿงฎ 4. Another Base to Decimal

Multiply every digit by the corresponding power of the source base, beginning with power 0 at the rightmost digit, and add the results.

Convert (101101)2 to decimal

Expand using powers of 2.

Positional expansion

1ร—25 + 0ร—24 + 1ร—23 + 1ร—22 + 0ร—21 + 1ร—20

32 + 0 + 8 + 4 + 0 + 1 = 45

(101101)2 = (45)10

3๏ธโƒฃ 5. Binary and Octal Conversion

One octal digit represents exactly three binary bits because 8 = 23. Group binary digits in sets of three from the radix point outwards.

Binary โ†’ Octal

(110 101 011)2

110 = 6, 101 = 5, 011 = 3

(653)8
Octal โ†’ Binary

(572)8

5 = 101, 7 = 111, 2 = 010

(101111010)2
Add leading or trailing zeroes only when necessary to complete a group of three. These zeroes do not change the value.

4๏ธโƒฃ 6. Binary and Hexadecimal Conversion

One hexadecimal digit represents four binary bits because 16 = 24. Group binary digits in sets of four.

Binary โ†’ Hexadecimal

(1011 1100 0110)2

1011 = B, 1100 = C, 0110 = 6

(BC6)16
Hexadecimal โ†’ Binary

(3AF)16

3 = 0011, A = 1010, F = 1111

(001110101111)2

๐Ÿ” 7. Octal and Hexadecimal Conversion

There is no direct digit grouping between octal and hexadecimal. Use binary as the bridge.

Octal โ†’ 3-bit binary groups โ†’ regroup into 4 bits โ†’ Hexadecimal

Convert (725)8 to hexadecimal

Step 1: Octal to binary

7 = 111, 2 = 010, 5 = 101

(725)8 = (111010101)2

Step 2: Regroup into four bits

0001 1101 0101 = 1 D 5

(725)8 = (1D5)16

๐Ÿ”น 8. Converting Fractional Parts

For a decimal fraction, repeatedly multiply the fractional part by the target base. Record the integer part produced at each step and read the recorded digits from top to bottom.

Convert (0.625)10 to binary

Repeated multiplication

0.625 ร— 2 = 1.250 โ†’ digit 1

0.250 ร— 2 = 0.500 โ†’ digit 0

0.500 ร— 2 = 1.000 โ†’ digit 1

(0.625)10 = (0.101)2

Some fractions repeat forever in another base. In such cases, stop at the required precision and mark the result as approximate.

๐Ÿงช 9. Base Conversion Explorer

Enter a valid integer or fractional value, choose the source and target bases, and view both the result and the conversion method.

Select an example or enter a number, then click Convert Number.

๐Ÿงฎ 10. Solved Problems

Problem 1

Convert (156)10 to hexadecimal.

Solution

156 รท 16 = 9, remainder 12 = C

9 รท 16 = 0, remainder 9

(156)10 = (9C)16

Problem 2

Convert (347)8 to decimal.

Solution

3ร—82 + 4ร—81 + 7ร—80

192 + 32 + 7 = 231

(347)8 = (231)10

Problem 3

Convert (2D7)16 to decimal.

Solution

2ร—162 + 13ร—161 + 7ร—160

512 + 208 + 7 = 727

(2D7)16 = (727)10

โš ๏ธ 11. Common Mistakes and Quick Checks

Using an invalid digit

Binary cannot contain 2; octal cannot contain 8 or 9.

Reading remainders downwards

For repeated division, read remainders from bottom to top.

Grouping from the wrong side

Group integer bits from right to left and fractional bits from left to right.

Forgetting hexadecimal values

Remember that Aโ€“F represent decimal values 10โ€“15.

Verification method: Convert your final answer back to decimal. Both representations must produce the same decimal value.

โœ๏ธ 12. Practice Problems

Solve each conversion yourself before opening the solution.

1. Convert (93)10 to binary.
2. Convert (11010110)2 to hexadecimal.
3. Convert (642)8 to binary.
4. Convert (B4)16 to decimal.
5. Convert (0.375)10 to binary.

๐Ÿ“ Quick Revision

  • A base-b system uses digits from 0 to bโˆ’1.
  • Decimal to another base uses repeated division for integers.
  • Another base to decimal uses positional expansion.
  • Three binary bits correspond to one octal digit.
  • Four binary bits correspond to one hexadecimal digit.
  • Use binary as the bridge between octal and hexadecimal.
  • Repeated multiplication converts decimal fractional parts.
  • Convert back to decimal to verify the result.

๐Ÿ”„ Base Conversions Practice

You have learned, solved and revised the topic. Answer all 20 questions from easy to hard and submit once. Your score, correct answers and explanations appear after submission.

Start 20-Question Test โ†’