C++ Program For Binary To Decimal Conversion
Last Updated :
04 Aug, 2023
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
- Initialize a variable dec_value to store the decimal representation and a variable base to keep track of the current binary place.
- Run a loop till num is non-zero,
- Extract the last digit of num and store it in a variable last_digit.
- Update num by removing the last digit.
- Add last_digit * base (power of 2) to dec_value to calculate the decimal value of the current binary place.
- Update the base by multiplying it by 2.
- Return dec_value as it holds the decimal representation of the binary number.
Example
The below diagram explains how to convert ( 1010 ) to an equivalent decimal value.
C++ Program to Convert Binary Numbers to Decimal
C++
#include <iostream>
using namespace std;
int binaryToDecimal( int n)
{
int num = n;
int dec_value = 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;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
|
Complexity Analysis
- Time complexity : O(log n)
- Auxiliary Space : O(1)
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++
#include <iostream>
#include <string>
using namespace std;
int binaryToDecimal(string n)
{
string num = n;
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;
}
return dec_value;
}
int main()
{
string num = "10101001" ;
cout << binaryToDecimal(num) << endl;
}
|
Complexity Analysis
- Time complexity: O(n) where n is the length of the string.
- Auxiliary Space : O(1)
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.
- to_ulong(): Converts bitset to unsigned long.
- to_ullong(): Converts bitset to unsigned long long.
It is defined inside <bitset> header file.
C++ Program to Convert Binary Numbers to Decimal Using std::bitset
C++
#include <bitset>
#include <iostream>
#include <string>
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;
}
|
Complexity Analysis
- Time complexity: O(n) where n is the number of bits.
- Auxiliary Space : O(1)
Refer to the complete article Program for Binary To Decimal Conversion for more methods to convert binary to decimal.
Related Articles
Share your thoughts in the comments
Please Login to comment...