Open In App

C++ Program For Binary To Decimal Conversion

The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++.

Algorithm to Convert Binary Numbers to Decimal

Example

The below diagram explains how to convert ( 1010 ) to an equivalent decimal value.



C++ Program to Convert Binary Numbers to Decimal




// C++ program to convert binary
// to decimal
#include <iostream>
using namespace std;
  
// Function to convert binary
// to decimal
int binaryToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
  
    // Initializing base value to
    // 1, i.e 2^0
    int base = 1;
  
    int temp = num;
    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;
        dec_value += last_digit * base;
        base = base * 2;
    }
  
    return dec_value;
}
  
// Driver code
int main()
{
    int num = 10101001;
    cout << binaryToDecimal(num) << endl;
}

Output
169

Complexity Analysis

Note: The program works only with binary numbers in the range of integers. In case you want to work with long binary numbers like 20 bits or 30 bits, you can use a string variable to store the binary numbers.



Below is a similar program that uses string variables instead of integers to store binary values.




// C++ program to convert binary to decimal
// when input is represented as binary string.
#include <iostream>
#include <string>
using namespace std;
  
// Function to convert binary
// to decimal
int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
  
    // Initializing base value to 1, i.e 2^0
    int base = 1;
  
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
  
    return dec_value;
}
  
// Driver code
int main()
{
    string num = "10101001";
    cout << binaryToDecimal(num) << endl;
}

Output
169

Complexity Analysis

Convert Binary Numbers to Decimal Using std::bitset Class

In C++, the std::bitset class provides an easy way to work with binary numbers. It has the following member functions to convert Binary Numbers to Decimals.

It is defined inside <bitset> header file.

C++ Program to Convert Binary Numbers to Decimal Using std::bitset




// C++ program that demonstrates how to convert binary
// numbers to decimal using std::bitset class
  
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string binary_string = "1101";
    // Assuming 4-bit integer, adjust the size as needed
    bitset<4> bits(binary_string);
  
    unsigned long decimal_value = bits.to_ulong();
    cout << decimal_value << endl;
  
    return 0;
}

Output
13

Complexity Analysis

Refer to the complete article Program for Binary To Decimal Conversion for more methods to convert binary to decimal.

Related Articles


Article Tags :