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
C program to convert binary number to decimal number
We will continue doing this until we process all the bits in the binary number. Let us understand this logic with the help of the below code example.
#include <stdio.h>
#include <math.h>
int BinToDecimal(long long);
int main() {
long long binNum;
printf("Please enter 8 bit binary number: ");
scanf("%lld", &binNum);
printf("Decimal Equivalent to Binary number = %lld is = %d \n", binNum,BinToDecimal(binNum));
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: 101010
Decimal Equivalent to Binary number = 101010 is = 42
C program to convert binary to decimal
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 <stdio.h>
#include <math.h>
#include <string.h>
#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;
printf("Please enter 16 bit (Positive or Negative) binary number: ");
gets(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;
}
printf("Decimal Equivalent to Binary number = %s is = %lld \n", binNum,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 <stdio.h>
#include <math.h>
#include <string.h>
#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;
printf("Please enter 16 bit (Positive or Negative) binary number: ");
gets(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;
}
printf("Decimal Equivalent to Binary number = %s is = %lld \n", binNum,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 <stdio.h>
#include <math.h>
#include <string.h>
#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;
printf("Please enter 16 bit (Positive or Negative) binary number: ");
gets(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;
}
printf("Decimal Equivalent to Binary number = %s is = %lld \n", binNum,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: 1010101010101010
Decimal Equivalent to Binary number = 1010101010101010 is = -21846
Please enter 16 bit (Positive or Negative) binary number: 0101010101010101
Decimal Equivalent to Binary number = 0101010101010101 is = 21845