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โท |
| 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 |
Program Code
1#include <stdio.h>2#include <math.h>34int 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 digit15 decimal += digit * pow(2, i); // Add position value16 binary /= 10; // Remove rightmost digit17 i++; // Move to next position18 }19 20 printf("Decimal of %lld: %d\n", original, decimal);21 22 return 0;23}Enter a binary number: 11001
Decimal of 11001: 25
Code Explanation
digit = binary % 10Gets 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 /= 10Remove the processed digit by dividing by 10.
๐ฏ Key Takeaways
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials