๐Ÿ”„๐Ÿ“šIntermediate

Binary to Decimal Conversion

Convert binary to decimal

Convert binary to decimal by multiplying each bit by its position value (power of 2). Each position from right to left represents 2โฐ, 2ยน, 2ยฒ, etc.

๐Ÿ“ Formula

For binary number bโ‚ƒbโ‚‚bโ‚bโ‚€:

Decimal = bโ‚€ร—2โฐ + bโ‚ร—2ยน + bโ‚‚ร—2ยฒ + bโ‚ƒร—2ยณ + ...

๐Ÿ”ข Step-by-Step: Converting 11001 to Decimal

Binary: 1 1 0 0 1

Position: 4 3 2 1 0

= 1ร—2โด + 1ร—2ยณ + 0ร—2ยฒ + 0ร—2ยน + 1ร—2โฐ

= 1ร—16 + 1ร—8 + 0ร—4 + 0ร—2 + 1ร—1

= 16 + 8 + 0 + 0 + 1

= 25

๐Ÿ“Š Powers of 2 Reference

2โฐ2ยน2ยฒ2ยณ2โด2โต2โถ2โท
1248163264128

Program Code

binary_decimal.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 long long binary;
6 int decimal = 0, i = 0;
7
8 printf("Enter a binary number: ");
9 scanf("%lld", &binary);
10
11 long long original = binary;
12
13 while (binary > 0) {
14 int digit = binary % 10; // Get rightmost digit
15 decimal += digit * pow(2, i); // Add position value
16 binary /= 10; // Remove rightmost digit
17 i++; // Move to next position
18 }
19
20 printf("Decimal of %lld: %d\n", original, decimal);
21
22 return 0;
23}
Output

Enter a binary number: 11001

Decimal of 11001: 25

Code Explanation

digit = binary % 10

Gets the rightmost digit (0 or 1) from the input. Note: Input is read as decimal number with only 0s and 1s.

decimal += digit * pow(2, i)

Multiply digit by 2โฑ and add to result. Position i starts at 0 (rightmost).

binary /= 10

Remove the processed digit by dividing by 10.

๐ŸŽฏ Key Takeaways

โœ“Each bit position = power of 2
โœ“Rightmost position = 2โฐ = 1
โœ“Use % 10 to extract digits
โœ“pow(2, i) for position value

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples