In this article, we are going to learn the C program to find twos complement of binary number entered by the user. A binary number consists of 0s and 1s in its composition. Two’s complement is the way every computer uses to represent negative integers. To get the two’s complement negative notation of an integer, you write out the number in binary. You then invert the digits and add one to the result.
C program to find twos complement of a binary number
In simple term 2’s Complement is a way to store negative numbers in Computer Memory. Whereas Positive Numbers are stored as Normal Binary Numbers. We are going to take input from the user as a binary number and then we will iterate over this number one bit at a time and calculate the one complements of this number.
Then we need to just add 1 to this one complement to get the two’s complement of the given binary number.Let us see this logic in action in the below code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
int i, carry=1;
printf("Please enter 8 bit binary number : ");
scanf("%s",binNum);
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
printf("You entered a Non-Binary Number.Exiting Program");
exit(0);
}
}
binComp[8] = '
#include <stdio.h>
#include <stdlib.h>
int main()
{
char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
int i, carry=1;
printf("Please enter 8 bit binary number : ");
scanf("%s",binNum);
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
printf("You entered a Non-Binary Number.Exiting Program");
exit(0);
}
}
binComp[8] = '\0';
// Calculate twos complement by adding 1
for(int i = 8-1; i >= 0; i--)
{
if(binComp[i] == '1' && carry == 1)
{
bin2sComp[i] = '0';
}
else if(binComp[i] == '0' && carry == 1)
{
bin2sComp[i] = '1';
carry = 0;
}
else
{
bin2sComp[i] = binComp[i];
}
}
bin2sComp[8] = '\0';
printf("Ones complement of %s is %s",binNum, binComp);
printf("\nTwos complement of %s is %s",binNum, bin2sComp);
return 0;
}
';
// Calculate twos complement by adding 1
for(int i = 8-1; i >= 0; i--)
{
if(binComp[i] == '1' && carry == 1)
{
bin2sComp[i] = '0';
}
else if(binComp[i] == '0' && carry == 1)
{
bin2sComp[i] = '1';
carry = 0;
}
else
{
bin2sComp[i] = binComp[i];
}
}
bin2sComp[8] = '
#include <stdio.h>
#include <stdlib.h>
int main()
{
char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
int i, carry=1;
printf("Please enter 8 bit binary number : ");
scanf("%s",binNum);
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
printf("You entered a Non-Binary Number.Exiting Program");
exit(0);
}
}
binComp[8] = '\0';
// Calculate twos complement by adding 1
for(int i = 8-1; i >= 0; i--)
{
if(binComp[i] == '1' && carry == 1)
{
bin2sComp[i] = '0';
}
else if(binComp[i] == '0' && carry == 1)
{
bin2sComp[i] = '1';
carry = 0;
}
else
{
bin2sComp[i] = binComp[i];
}
}
bin2sComp[8] = '\0';
printf("Ones complement of %s is %s",binNum, binComp);
printf("\nTwos complement of %s is %s",binNum, bin2sComp);
return 0;
}
';
printf("Ones complement of %s is %s",binNum, binComp);
printf("\nTwos complement of %s is %s",binNum, bin2sComp);
return 0;
}
Output
Please enter 8 bit binary number : 10101010
Ones complement of 10101010 is 01010101
Twos complement of 10101010 is 01010110
Please enter 8 bit binary number : 10101023
You entered a Non-Binary Number.Exiting Program