Convert Binary to Decimal in C++

C++ Program to Convert Binary to Decimal (5 Ways With Output).

IntermediateAdvanced Number ProgramsExample 5 of 13
convert-binary-to-decimal.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int main() {
6 long long binary;
7 int decimal = 0, i = 0, remainder;
8
9 cout << "Enter a binary number: ";
10 cin >> binary;
11
12 long long temp = binary;
13
14 while (temp != 0) {
15 remainder = temp % 10;
16 temp /= 10;
17 decimal += remainder * pow(2, i);
18 ++i;
19 }
20
21 cout << "Binary: " << binary << " = Decimal: " << decimal << endl;
22
23 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:

Input binary: 1010
Output decimal: 10

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:

1010 (binary) = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10 (decimal)
1101 (binary) = 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 8 + 4 + 0 + 1 = 13 (decimal)

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides pow() function for calculating powers.
Essential for computing 2 raised to various powers.

Declaring Variables

The program declares:

long long binary;

int decimal = 0, i = 0, remainder;

binary stores the binary number entered by the user (long long for large binary numbers).
decimal stores the converted decimal result (initialized to 0).
i tracks the position/index of the current digit (starts at 0 for rightmost digit).
remainder stores the last digit extracted in each iteration.

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:

remainder = 1010 % 10 = 0 (extract rightmost digit)
temp = 1010 / 10 = 101 (remove rightmost digit)
decimal = 0 + 0 × 2⁰ = 0 + 0 = 0
i = 1 (move to next position)

Step 2:

remainder = 101 % 10 = 1
temp = 101 / 10 = 10
decimal = 0 + 1 × 2¹ = 0 + 2 = 2
i = 2

Step 3:

remainder = 10 % 10 = 0
temp = 10 / 10 = 1
decimal = 2 + 0 × 2² = 2 + 0 = 2
i = 3

Step 4:

remainder = 1 % 10 = 1
temp = 1 / 10 = 0
decimal = 2 + 1 × 2³ = 2 + 8 = 10
i = 4

Step 5:

temp is now 0, so the loop stops
Final result: decimal = 10

Understanding the Binary System

Positional Value System

:

In binary, each position represents a power of 2:

Rightmost position (position 0): 2⁰ = 1
Next position (position 1): 2¹ = 2
Next position (position 2): 2² = 4
Next position (position 3): 2³ = 8
And so on...

Reading Binary from Right to Left

:

Position 0 (rightmost): units place (2⁰ = 1)
Position 1: twos place (2¹ = 2)
Position 2: fours place (2² = 4)
Position 3: eights place (2³ = 8)

Example Calculation

:

Binary 1010:

Digit at position 0: 0 → contributes 0 × 1 = 0
Digit at position 1: 1 → contributes 1 × 2 = 2
Digit at position 2: 0 → contributes 0 × 4 = 0
Digit at position 3: 1 → contributes 1 × 8 = 8
Total: 0 + 2 + 0 + 8 = 10

Key Operations Explained

Modulo Operator (%)

:

temp % 10 extracts the rightmost digit.
Example: 1010 % 10 = 0, 101 % 10 = 1

Integer Division (/)

:

temp / 10 removes the rightmost digit.
Example: 1010 / 10 = 101, 101 / 10 = 10

Power Function (pow())

:

pow(2, i) calculates 2 raised to the power i.
Example: pow(2, 0) = 1, pow(2, 1) = 2, pow(2, 3) = 8

Accumulation

:

decimal += remainder * pow(2, i) adds the contribution of current digit.
Each digit's contribution is: digit × 2^position

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;

}

Uses base variable that doubles each iteration (1, 2, 4, 8, ...).
Avoids pow() function calls - more efficient.

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);

}

Converts binary to string.
Processes each character from left to right.

Method 4: Using Recursion

int binaryToDecimal(long long binary, int i = 0) {

if (binary == 0) return 0;

}

Recursive approach - function calls itself.
Base case: when binary becomes 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);

stoi() can convert string to integer with specified base.
Base 2 means binary conversion.
Simplest method but requires string conversion.

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)

:

Uses digits 0-9.
Each position is a power of 10.
Example: 123 = 1×10² + 2×10¹ + 3×10⁰

Binary (Base-2)

:

Uses digits 0-1 only.
Each position is a power of 2.
Example: 1010 = 1×2³ + 0×2² + 1×2¹ + 0×2⁰

Why Binary?

:

Computers use binary because electronic circuits have two states: on (1) and off (0).
All data in computers is ultimately stored in binary format.
Understanding binary is essential for computer science.

Common Binary Values

Powers of 2

:

2⁰ = 1
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16
2⁵ = 32
2⁶ = 64
2⁷ = 128

Common Binary to Decimal

:

0 (binary) = 0 (decimal)
1 (binary) = 1 (decimal)
10 (binary) = 2 (decimal)
11 (binary) = 3 (decimal)
100 (binary) = 4 (decimal)
101 (binary) = 5 (decimal)
1111 (binary) = 15 (decimal)
10000 (binary) = 16 (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

:

Binary numbers can only contain 0 and 1.
This program doesn't validate input - assumes valid binary.
Invalid digits (2-9) would produce incorrect results.

Large Binary Numbers

:

long long is used to handle large binary numbers.
Very large binary numbers may still cause overflow.
Consider using string for very large numbers.

Reading Direction

:

Binary is read from right to left (least significant to most significant).
Position 0 is the rightmost digit (ones place).

return 0;

This ends the program successfully.