Convert Binary to Decimal in C++
Convert a binary number typed as digits like 1010 into decimal in C++ by multiplying each bit by a power of 2. Covers the long long input trick and its limits.
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}
Sample Output
Binary: 1010 = Decimal: 10
Sum of 8 and 2.
In this one
- How place values work in base 2
- How % 10 reads binary digits from a number typed in decimal form
- How pow(2, i) gives each place value
- Why inputs longer than about 19 digits fail
How it works
- 1
Read 1010 into a long long.
- 2
Take the last digit with temp % 10.
- 3
Add digit × 2^i, then i++.
- 4
Bits 0,1,0,1 give 0 + 2 + 0 + 8 = 10.
- 5
Print the result.
Edge cases
Non-binary digit
Enter 1021.
The program does not check, so the 2 is treated as 2 × 2^1 and the answer is meaningless. Validate each digit is 0 or 1.
Very long binary
Enter a 25-digit binary.
It does not fit in long long (about 19 digits), so cin fails. Read binary as a string for long inputs.
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:
cout << "Enter a binary number: ";
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.
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;
return (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:
cout << "Binary: " << binary << " = Decimal: " << decimal << endl;
Output:
Binary: 1010 = Decimal: 10
This clearly shows both the original binary number and its decimal equivalent.
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
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is binary 1101 in decimal?
Pick an answer to check it.
Question 2
What does % 10 do to 1010 here?
Pick an answer to check it.
Questions people ask
Is there a library way?
stoi("1010", nullptr, 2) or bitset<32>("1010").to_ulong().
Why not use pow in a loop?
Doubling a running place value (place *= 2) avoids floating point and is simpler.
Why is decimal an int?
For inputs up to 19 binary digits the value fits easily in int.