C++ program to find 2s complement of binary number

In this article, we are going to learn how to find the twos Complement of a 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. 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 complement 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.

C++ program to find 2s complement of a binary number



#include <iostream>
using namespace std;

int main()
{
    char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
    int i, carry=1;

    cout <<"Please enter 8 bit binary number : ";
    cin >> binNum;

    for(int i = 0; i < 8; i++)
    {
        if(binNum[i] == '1')
        {
            binComp[i] = '0';
        }
        else if(binNum[i] == '0')
        {
            binComp[i] = '1';
        }
        else
	{
            cout<<"You entered a Non-Binary Number.Exiting Program";
            exit(0);
	}
    }
    binComp[8] = '
#include <iostream>
using namespace std;
int main()
{
char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
int i, carry=1;
cout <<"Please enter 8 bit binary number : ";
cin >> binNum;
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
cout<<"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';
cout<< "\n Ones complement of " << binNum << " is "<< binComp;
cout<< "\n Twos complement of " << binNum << " is "<< 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 <iostream>
using namespace std;
int main()
{
char binNum[8 + 1], binComp[8 + 1], bin2sComp[8 + 1];
int i, carry=1;
cout <<"Please enter 8 bit binary number : ";
cin >> binNum;
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
cout<<"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';
cout<< "\n Ones complement of " << binNum << " is "<< binComp;
cout<< "\n Twos complement of " << binNum << " is "<< bin2sComp;
return 0;
}
'; cout<< "\n Ones complement of " << binNum << " is "<< binComp; cout<< "\n Twos complement of " << binNum << " is "<< bin2sComp; return 0; }

Output

Please enter 8 bit binary number : 11111111

 Ones complement of 11111111 is 00000000
 Twos complement of 11111111 is 00000001