C++ program to Convert Binary to Decimal

In this article, we are going to learn how to convert a binary number to an equivalent decimal number. Here we are going to learn about two numbers one is binary and another is decimal. Binary numbers are base 2 numbers and represented in 0s and 1s. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9.

In this example, we are going to take the input from the user in the form of a binary number. Then we will start from the right side of this binary number and will check each bit one by one. If the bit is 1, we will add it to a decimal number by taking the power of 2 using the below formula:

decimalNumber += pow(2, N)

Parameters

  • where N is the position from right

1. C++ program to convert binary to decimal number


In this example program, we are going to use pow() function. pow() function is used to find power of a number.

#include <iostream>
#include <math.h>
using namespace std;

int BinToDecimal(long long);

int main() 
{
  long long binNum;
  cout <<"Please enter 8 bit binary number : ";
  cin >> binNum;
  
  long decimalNum = BinToDecimal(binNum);
  cout <<"Decimal Equivalent to Binary number = " << binNum <<" is "<< decimalNum ;

  return 0;
}


int BinToDecimal(long long binNo) 
{
  int decNum = 0;
  int index = 0;
  int remainder;

  while (binNo!=0) 
  {
    remainder = binNo % 10;
    binNo /= 10;
    decNum += remainder * pow(2, index);
    ++index;
  }

  return decNum;
}

Output

Please enter 8 bit binary number : 10101010
Decimal Equivalent to Binary number = 10101010 is 170

2. C++ program to convert binary to decimal number


In the above example, we learned how to convert a binary number to a decimal. But that program is capable of converting positive numbers only. Because we are not checking if the binary number is positive or negative. In the example, we will learn how to check if the binary value is negative. First, we will check the first bit of the binary number to find out if it is positive or negative. If the number is negative, We will find its two’s complement. Then convert it to decimal and then we will then multiply it by -1 to represent it negatively in decimal. Let us see this logic in the below code.

#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;

#define BINSIZE 16 

void FindTwosComplement(char *twosComp, const char *);

int main()
{
    char binNum[BINSIZE + 1];
    char binTemp[BINSIZE + 1];

    int  signBit = 0;
    long long decNum = 0;

    cout<< "Please enter 16 bit (Positive or Negative) binary number: ";
    cin >> binNum;

    strcpy(binTemp, binNum);

    if(binNum[0] == '1')
    {
        signBit = 1;
        FindTwosComplement(binTemp, binNum);
    }

    for(int i=0; i< BINSIZE; i++)
    {
        if(binTemp[i] == '1')
        {
            decNum += pow(2, (BINSIZE - (i+1)));
        }
    }

    if(signBit==1)
    {
        decNum *= -1;
    }

   cout <<"Decimal Equivalent to Binary number = " << binNum <<" is "<< decNum ;
   
   return 0;
}


void FindTwosComplement(char * twosComp, const char * binNo)
{
    char onesComp[BINSIZE + 1];
    int carry=1;

    for(int i=0; i< BINSIZE; i++)
    {
        if(binNo[i]=='1')
        {
            onesComp[i] = '0';
        }
        else if(binNo[i]=='0')
        {
            onesComp[i] = '1';
        }
    }
    onesComp[BINSIZE] = '
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
#define BINSIZE 16 
void FindTwosComplement(char *twosComp, const char *);
int main()
{
char binNum[BINSIZE + 1];
char binTemp[BINSIZE + 1];
int  signBit = 0;
long long decNum = 0;
cout<< "Please enter 16 bit (Positive or Negative) binary number: ";
cin >> binNum;
strcpy(binTemp, binNum);
if(binNum[0] == '1')
{
signBit = 1;
FindTwosComplement(binTemp, binNum);
}
for(int i=0; i< BINSIZE; i++)
{
if(binTemp[i] == '1')
{
decNum += pow(2, (BINSIZE - (i+1)));
}
}
if(signBit==1)
{
decNum *= -1;
}
cout <<"Decimal Equivalent to Binary number = " << binNum <<" is "<< decNum ;
return 0;
}
void FindTwosComplement(char * twosComp, const char * binNo)
{
char onesComp[BINSIZE + 1];
int carry=1;
for(int i=0; i< BINSIZE; i++)
{
if(binNo[i]=='1')
{
onesComp[i] = '0';
}
else if(binNo[i]=='0')
{
onesComp[i] = '1';
}
}
onesComp[BINSIZE] = '\0';
for(int i= BINSIZE-1; i>=0; i--)
{
if(onesComp[i]=='1' && carry==1)
{
twosComp[i] = '0';
}
else if(onesComp[i]=='0' && carry==1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[BINSIZE] = '\0';
}
'; for(int i= BINSIZE-1; i>=0; i--) { if(onesComp[i]=='1' && carry==1) { twosComp[i] = '0'; } else if(onesComp[i]=='0' && carry==1) { twosComp[i] = '1'; carry = 0; } else { twosComp[i] = onesComp[i]; } } twosComp[BINSIZE] = '
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
#define BINSIZE 16 
void FindTwosComplement(char *twosComp, const char *);
int main()
{
char binNum[BINSIZE + 1];
char binTemp[BINSIZE + 1];
int  signBit = 0;
long long decNum = 0;
cout<< "Please enter 16 bit (Positive or Negative) binary number: ";
cin >> binNum;
strcpy(binTemp, binNum);
if(binNum[0] == '1')
{
signBit = 1;
FindTwosComplement(binTemp, binNum);
}
for(int i=0; i< BINSIZE; i++)
{
if(binTemp[i] == '1')
{
decNum += pow(2, (BINSIZE - (i+1)));
}
}
if(signBit==1)
{
decNum *= -1;
}
cout <<"Decimal Equivalent to Binary number = " << binNum <<" is "<< decNum ;
return 0;
}
void FindTwosComplement(char * twosComp, const char * binNo)
{
char onesComp[BINSIZE + 1];
int carry=1;
for(int i=0; i< BINSIZE; i++)
{
if(binNo[i]=='1')
{
onesComp[i] = '0';
}
else if(binNo[i]=='0')
{
onesComp[i] = '1';
}
}
onesComp[BINSIZE] = '\0';
for(int i= BINSIZE-1; i>=0; i--)
{
if(onesComp[i]=='1' && carry==1)
{
twosComp[i] = '0';
}
else if(onesComp[i]=='0' && carry==1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[BINSIZE] = '\0';
}
'; }

Output

Please enter 16 bit (Positive or Negative) binary number: 10101010
Decimal Equivalent to Binary number = 10101010 is -22016

Please enter 16 bit (Positive or Negative) binary number: 01010101
Decimal Equivalent to Binary number = 01010101 is 21760