What is Decimal numbers?
These are the numbers which we generally use, like 6, 9875, 123, etc. They have a base of 10. The decimal numbers consist of numerals from 0 to 9.
The logic for Converting Binary Numbers to Decimal
Before we move on to discuss the code implementation of converting a binary number into a decimal number, let us first discuss the logic that we shall use in the code while converting binary to decimal.
- We start from the least significant bit (LSB), i.e., from the right-hand side of the binary number, and travel towards the most significant bit (MSB), i.e., towards the left.
- The position of the rightmost bit is considered zero, and every time we move towards the left, the position is increased by 1.
- For every bit (the value is either 0 or 1), we multiply that bit with 2 raised to the power of the position of that bit and add it to the result.
- We multiply it with the power of 2 because the base of the binary number is 2.
Consider the below image to understand it in a better way.
We have a binary number 101001. In order to convert it into decimal, we start with the rightmost bit. Here the rightmost bit is 1 at position 0. Therefore the result at this level is 1 X 20 = 1. For the second bit, the result is 0. We simply have to sum up the result at every step. The sum gives us the decimal number.
Also Read - C++ Interview Questions
C++ Program to Convert Binary Number to Decimal
Input: A binary number consisting of only 0s and 1s.
Expected output: The decimal form of the binary number provided in the input.
Code
// C++ program to convert binary number to Decimal
#include <iostream>
#include<cmath>
using namespace std;
// this function receives a number in binary form
// and returns the corresponding value in decimal form
int binaryToDecimal(int num){
int temp= num;
// position variable to keep track of the position of the bits
// from right to left, position increases from 0, 1, 2,...,n
int position=0;
// result variable stores the sum after every bit is multiplied
// by the 2 raised to the power of the position of the corresponding bit
int result=0;
while(temp>0){
// this will give the rightmost digit
// e.g. 36748%10 will give 8.
int rightMostBit = temp%10;
result += rightMostBit * pow(2, position);
// increment position as we travel from right to left
position++;
// this will remove the rightmost digit
// e.g. 36748/10 = 3674
temp= temp/10;
}
// return the calculated result
return result;
}
// main function where the code is tested
int main()
{
int num;
cout<<"Enter the Binary Number.\n";
cin>>num;
int temp= binaryToDecimal(num);
cout<<"The Decimal form of the given binary number is : "<< temp;
return 0;
}
You can also try this code with Online C++ Compiler
Run CodeInput
101001
Output
Enter the Binary Number.
101001
The Decimal form of the given binary number is : 41
You can also practice with the help of Online C++ Compiler
Time Complexity
The time complexity of the above code is O(n), where n is the number of digits in the input binary number. This is because the loop runs for n times, for every digit of the binary number given.
Space Complexity
The space complexity of the above code is O(1) because there is no extra space used in the code.
If The Input is In The Form of String
Since the value passed in the binaryToDecimal() function is an integer we cannot provide a very large value of the binary number to the function. Consider an example where we have, say, 25 bits in a binary number. An integer type variable cannot store it. The best way is to take such a number in string format as input and do computation and accordingly convert it to decimal.
Code
// C++ program to convert binary string to Decimal
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// this function receives a string in binary form
// and returns the corresponding integer value in decimal form
int binaryToDecimal(string binNum){
string num = binNum;
// position variable to keep track of the position of the bits
// from right to left, position increases from 0, 1, 2,...,n
int position = 0;
int result=0;
// length() determines the length of the given string
int len = num.length();
for(int i= len-1; i>=0;i--){
// whenever we encounter 1, we can simply add power of 2 raised to the position
// for 0 the multiplication with power of 2 will be zero
if(num[i]=='1'){
result+= pow(2,position);
}
// increment the position variable
position++;
}
// return the result
return result;
}
int main() {
string binaryNum;
cout<<"Enter the binary number.\n";
cin>>binaryNum;
int decimal = binaryToDecimal(binaryNum);
cout<<"The decimal form is : "<<decimal;
return 0;
}
You can also try this code with Online C++ Compiler
Run CodeInput
101001
Output
Enter the binary number.
101001
The decimal form is : 41
Time Complexity
The time complexity of the above code is O(n), where n is the number of characters in the input string of the binary number. It is because the for-loop runs for the number of times equal to the number of characters in the input string.
Space Complexity
The space complexity of the above code is O(1) because there is no extra space used in the code.
Alternate Way to convert Binary to decimal in C++
Instead of using pow(2, position), we can use a base variable that will be multiplied by 2 after every iteration.
Code
// C++ program to convert binary string to Decimal
// without using pow() function
#include <iostream>
#include <string>
using namespace std;
// this function receives a string in binary form
// and returns the corresponding integer value in decimal form
int binaryToDecimal(string binNum){
string num = binNum;
// this represents the rightmost position.
// observe that we have assigned base=1 and not 0
// because at position =0
// pow(2,0)=1
int base= 1;
int result=0;
// length() determines the length of the given string
int len = num.length();
for(int i= len-1; i>=0;i--){
// whenever we encounter 1, we can simply the value of base variable
if(num[i]=='1'){
result+= base;
}
// multiply base by 2 after every iteration
base= base *2;
}
// return the result
return result;
}
int main() {
string binaryNum;
cout<<"Enter the binary number.\n";
cin>>binaryNum;
int decimal = binaryToDecimal(binaryNum);
cout<<"The decimal form is : "<<decimal;
return 0;
}
You can also try this code with Online C++ Compiler
Run CodeInput
111100010100000011111
Output
Enter the binary number.
111100010100000011111
The decimal form is : 1976351
Time Complexity
The time complexity of the above code is O(n), where n is the number of characters in the input string of the binary number. It is because the for-loop runs for the number of times equal to the number of characters in the input string.
Space Complexity
The space complexity of the above code is O(1) because there is no extra space used in the code.
Also check out this article - Pair in C++
Using Predefined Function
We can use the stoi() function in C++ to convert a binary number given as a string into a decimal number.
Code
// using stoi() in C++ to convert binary to decimal
#include <iostream>
using namespace std;
int main() {
string binNum;
cout<<"Enter a binary number.\n";
cin>> binNum;
// C++ stoi() convert binary string to decimal
int decimal = stoi(binNum, 0, 2);
cout<<"The decimal form is : "<<decimal;
return 0;
}
You can also try this code with Online C++ Compiler
Run CodeInput
1001001110
Output
Enter a binary number.
1001001110
The decimal form is : 590
Time Complexity
The time complexity of the stoi() function is O(n), where n is the number of characters in the input string of the binary number.
Space Complexity
The space complexity of the above code is O(1) because there is no extra space used in the code.
Must read decimal to binary c++ and Application of Oops
Frequently Asked Questions
What is a binary number?
The binary number system is made up of two different numerals, zero and one. All numbers can be represented using 0 and 1.
What is the base of binary numbers?
The base of binary numbers is 2.
What is a decimal number?
These are the numbers which we generally use like 6, 9875, 123, etc. They have a base of 10. The decimal numbers consist of numerals from 0 to 9.
What will be the value of 1101101010 in the decimal system?
874
Conclusion
In this article, we have discussed what binary numbers are and the C++ program to convert binary numbers to decimal numbers. You should definitely read this blog to get a more clear understanding of number systems.
Refer to our guided paths on Code360to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in contests hosted on Code Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must have a look at the problems, interview experiences, and interview bundle for placement preparations.