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++
// 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;
}
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++
// 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;
}
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++
// 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;
}
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
Similar Reads
C++ Program For Decimal To Binary Conversion Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recomm
3 min read
C++ Program For Decimal To Octal Conversion The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value. In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octa
2 min read
C++ Program For Octal To Decimal Conversion Given an octal number as input, we need to write a program to convert the given octal number into an equivalent decimal number. Examples: Input : 67Output: 55 Input : 512Output: 330 Input : 123Output: 83 1. Simple ApproachThe idea is to extract the digits of a given octal number starting from the ri
2 min read
C++ Program For Binary To Octal Conversion The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple
5 min read
C++ Program For Hexadecimal To Decimal Conversion The hexadecimal numbers are base 16 numbers that use 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all digits. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). Decimal numbers are base 10 numbers with 10 symbols to represent all digits. In this article, we will l
3 min read