C++ Program For Binary To Decimal Conversion Last Updated : 04 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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 DecimalInitialize 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. Recommended PracticeBinary number to decimal numberTry It!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; } Output169 Complexity AnalysisTime 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; } Output169 Complexity AnalysisTime 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; } Output13 Complexity AnalysisTime 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 ArticlesConvert Binary Fraction to DecimalC++ Program For Decimal To Binary ConversionDecimal to binary conversion without using arithmetic operatorsDecimal to binary number using recursion Comment More infoAdvertise with us Next Article C++ Program For Binary To Decimal Conversion kartik Follow Improve Article Tags : C++ Programs C++ C++ Conversion Programs Practice Tags : CPP 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 C++ Program For Decimal To Hexadecimal Conversion In this article, we will learn to write a C++ program to convert a decimal number into an equivalent hexadecimal number. i.e. convert the number with base value 10 to base value 16. In the decimal system, we use ten digits (0 to 9) to represent a number, while in the hexadecimal system, we use sixte 3 min read Recursive Program for Binary to Decimal Given a binary number as string, find its decimal equivalent. Examples: Input : binary = "101"Output : 5 Input : binary = "1111"Output : 15 We have discussed iterative solution to convert Binary to Decimal.The idea is simple, we add current term and recur for remaining terms. C++ // Recursive CPP pr 3 min read C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read C++ Program For Double to String Conversion Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76 2 min read C++ Program to Convert Scientific Notation to Decimal Here, we will see how to convert scientific notations to decimals using a C++ program. Example: 1e9 = 1000000000 1e9 = 1000000000.000000 Calculate power using the pow(a,b) function Header File used: <math.h> pow also referred to as the power of function. Here, pow(a,b) refers to a power b. Let 3 min read Like