C++ Program For Binary To Decimal Conversion

Last Updated : 31 Mar, 2026

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++.

Steps

  • Initialize dec_value to store the decimal result and base to represent the current power of 2.
  • Execute a loop while num is non-zero.
  • Extract the last digit of num and store it in last_digit.
  • Remove the last digit from num.
  • Add last_digit × base to dec_value.
  • Update base by multiplying it by 2.
  • Return dec_value as the decimal equivalent of the binary number.

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

binary to decimal conversion in c++
Conversion of binary number 1010 into its decimal equivalent (10).

1. Using a loop

C++
#include <iostream>
#include <string>
using namespace std;

int main(){
    string num = "0001010";
    int dec_value = 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;
    }

    cout << dec_value << endl;
}

Output
10

Explanation:

  • initializes "dec_value" to store the decimal result.
  • "base" is used to represent powers of 2 (1, 2, 4, 8, …).
  • in each loop iteration, the last binary digit is extracted.
  • that digit is multiplied by the current base and added to dec_value.
  • the base is doubled for the next binary place.

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.

2. Program to Handle Large Binary Input Using Strings

C++
#include <iostream>
#include <string>
using namespace std;

int main(){
    string num = "10101001";
     int dec_value = 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;
    }
    cout<<dec_value<<endl;
}

Output
169

Explanation:

  • "dec_value" is used to store the decimal result.
  • "base" represents powers of 2 starting from 1.
  • loop starts from the last character of the string (least significant bit).
  • if the current character is '1', the current base value is added to dec_value.
  • base is doubled in each iteration to move to the next binary place.

3. 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.

  • to_ulong(): Converts bitset to unsigned long.
  • to_ullong(): Converts bitset to unsigned long long.

Note: It is defined inside <bitset> header file.

C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    string binary_string = "1101";
    bitset<4> bits(binary_string);
    unsigned long decimal_value = bits.to_ulong();
    cout << decimal_value << endl;
    return 0;
}

Output
13

Explanation:

  • a bitset<4> object is created to represent a 4-bit binary number using this string.
  • "to_ulong()" converts the binary value stored in the bitset into its decimal equivalent.
  • converted decimal value is stored in decimal_value.
Comment