Convert Binary to Decimal in C++
C++ Program to Convert Binary to Decimal (5 Ways With Output).
1#include <iostream>2#include <cmath>3using namespace std;45int main() {6 long long binary;7 int decimal = 0, i = 0, remainder;89 cout << "Enter a binary number: ";10 cin >> binary;1112 long long temp = binary;1314 while (temp != 0) {15 remainder = temp % 10;16 temp /= 10;17 decimal += remainder * pow(2, i);18 ++i;19 }2021 cout << "Binary: " << binary << " = Decimal: " << decimal << endl;2223 return 0;24}
Output
Enter a binary number: 1010 Binary: 1010 = Decimal: 10
What's going on
The program converts a binary number (base-2) to a decimal number (base-10). For example:
Binary numbers use only digits 0 and 1, while decimal numbers use digits 0-9. The conversion involves multiplying each binary digit by 2 raised to its position power and summing the results.
Example:
Header Files Used
Declaring Variables
The program declares:
long long binary;
int decimal = 0, i = 0, remainder;
Taking Input From the User
The program asks:
cin >> binary;
The user enters a binary number, for example: 1010
We also create a temporary copy:
long long temp = binary;
This is important because we'll modify the number during conversion, and we need the original for display.
1cout << "Enter a binary number: ";
The Conversion Algorithm
The core conversion uses a while loop:
while (temp != 0) {
remainder = temp % 10;
temp /= 10;
decimal += remainder * pow(2, i);
++i;
}
Let's break down how this works step-by-step for binary = 1010:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Understanding the Binary System
Positional Value System
:
In binary, each position represents a power of 2:
Reading Binary from Right to Left
:
Example Calculation
:
Binary 1010:
Key Operations Explained
Modulo Operator (%)
:
Integer Division (/)
:
Power Function (pow())
:
Accumulation
:
Other Methods (Mentioned but not shown in code)
Method 2: Using Bit Manipulation
int decimal = 0;
int base = 1;
while (binary > 0) {
int lastDigit = binary % 10;
decimal += lastDigit * base;
base *= 2;
binary /= 10;
}
Method 3: Using String
string binaryStr = to_string(binary);
int decimal = 0;
for (int i = 0; i < binaryStr.length(); i++) {
int digit = binaryStr[i] - '0';
decimal += digit * pow(2, binaryStr.length() - 1 - i);
}
Method 4: Using Recursion
int binaryToDecimal(long long binary, int i = 0) {
if (binary == 0) return 0;
}
1return (binary % 10) * pow(2, i) + binaryToDecimal(binary / 10, i + 1);
Method 5: Using stoi() with Base 2
string binaryStr = to_string(binary);
int decimal = stoi(binaryStr, nullptr, 2);
Displaying the Result
The program prints:
Output:
Binary: 1010 = Decimal: 10
This clearly shows both the original binary number and its decimal equivalent.
1cout << "Binary: " << binary << " = Decimal: " << decimal << endl;
Understanding Number Bases
Decimal (Base-10)
:
Binary (Base-2)
:
Why Binary?
:
Common Binary Values
Powers of 2
:
Common Binary to Decimal
:
When to Use Each Method
-
pow() Method
: Good for learning - clear and straightforward.
-
Bit Manipulation
: Best for efficiency - avoids pow() overhead, recommended.
-
String Method
: Good for learning string operations - processes left to right.
-
Recursion
: Educational - helps understand recursive thinking.
-
stoi() Method
: Simplest - one line, but requires string conversion.
Best Practice
: Use bit manipulation method for efficiency, or stoi() for simplicity.
Important Considerations
Valid Binary Digits
:
Large Binary Numbers
:
Reading Direction
:
return 0;
This ends the program successfully.