Updated March 29, 2023
Introduction to 2s Complement in C
While move into 2’s complement, we will understand what is 1’s complement first. 1’s complement of any binary number will give you another binary number by changing all bits into its equivalent complement form, like 0’s will be transformed into 1s and 1’s transformed into 0s. Same way, 2’s complement is defined as 1 is added to the 1’s complement, then the resultant becomes 2’s complement. In this topic, we are going to learn about 2s Complement in C.
Real-Time Usage: Day to day life, we have observed so many color blinking lights with the exact time intervals. In this kind of situation, we have used these 1’s and 2’s complement. Why because each 1 represents switch on and each 0 represents switch off. The initial binary number gives one pattern of “switch on” and “switch off,” and 1’s and 2’s complement gives exactly opposite action pattern of “switch on” and “switch off” will observe. This leads to making us all lights are blinking.
Advantages:
- Useful in security encoding.
- Useful in festival continuous blinking lights.
How Does 2’s Complement Work in C Language?
Logic to implement 2’s complement:
- Ask the input number from the user, and store it in any variable assume “input.”
- Then find the 1’s complement of the input number.
- Consider 2 variables like two Complement and carryDigit=1.
- Iterate the loop from 0 to the actual size of the number.
- Mainly we have 3 cases inside the for a loop.
- If 2 variables 1’s complement and carryDigit are 1, then we can insert 0 to the 2’s complement.
- If 1’s complement is 0 and carryDigit bit is 1 then, we can insert 1 to 2’s complement and add the carrDigit value to 0.
- If the carryDigit is 0, then assign the value of 1’s complement to 2’s complement.
Syntax:
int oneComplement, twoComplement, CarryDigit=1;
for(int p = length - 1; p >= 0; p--)
{
ifoneComplement[p] == '1' && carryDigit == 1)
{
oneComplement[p] = '0';
}
else if(oneComplement[p] == '0' && carryDigit == 1)
{
twoComplement[p] = '1';
carryDigit = 0;
}
else
{
twoComplement[p] = oneComplement[p];
}
}
Examples of 2s Complement in C
Given below are the examples of 2s Complement in C:
Example #1
2’s complement with 8 digits binary number
Code:
#include<stdio.h>
#include<stdlib.h>
#define LENGTH 8
//main method for executing the code
int main()
{
//initialize carry variable
int carryDigit = 1;
//initialoze 1's and 2's complement variables
char input[LENGTH + 1], oneComplement[LENGTH + 1], twoComplement[LENGTH + 1];
//Ask user to enter 8 digit binary number
printf("Please enter 8 digit binary number=>\n");
gets(input);//equivalent to scanf method
//1's complement logic within for loop
for( int var= 0; var < LENGTH; var++)
{
if(input[var] == '0')
{
oneComplement[var] = '1';
}
else if(input[var] == '1')
{
oneComplement[var] = '0';
}
}
oneComplement[LENGTH] = '\0';
//2's complement logic within for loop
for(int var = LENGTH - 1; var >= 0; var--)
{
if(oneComplement[var] == '1' && carryDigit == 1)
{
twoComplement[var] = '0';
}
else if(oneComplement[var] == '0' && carryDigit == 1)
{
twoComplement[var] = '1';
carryDigit = 0;
}
else
{
twoComplement[var] = oneComplement[var];//say 1's and 2's complement are same
}
}
twoComplement[LENGTH] = '\0';
//printing 2's complement output
printf("2's complement of 8 digit binary number %s is=> %s\n",input, twoComplement);
return 0;
}
Output:
Example #2
Other way to do 2’s complement is by using the method, and the input range can be up to 16 digits
Code:
#include<stdio.h>//Provide basic C libraries
#include<conio.h>
#include<string.h>//Provide String library for String operations
#define LENGTH 16// declaring constant for using in the entire application
main()
{
//declaring integer variables
int i,test;
//declaring character array variables
char binaryArray[LENGTH],tempArray[LENGTH],a[LENGTH];
//declaring userInput method
void userInput(char binaryArray[]);
//declaring validate method
int validate(char binaryArray[]);
//declaring twosComplement method
void twosComplement(char binaryArray[],char a[]);
//calling userInput method
userInput(binaryArray);
//copying one array to other array
strcpy(tempArray,binaryArray);
//calling validate method
test=validate(binaryArray);
//checking wheter given value is valid or not from user
if(test==0)
{
printf("\nPlease enter valid binary number");
exit(1);
}
//calling twosComplement method
twosComplement(binaryArray,a);
printf("\n2's complement is %s",a);
getch();
}
//logic for userInput method for asking user user input
void userInput(char binaryArray[])
{
printf("Please enter the binary number maximum length of 16 \n");
scanf("%s",binaryArray);
return;
}
//validate the user input
int validate(char binaryArray[])
{
int i,l,x=1;
l=strlen(binaryArray);
for(i=0; i<l; i++)
{
if(!((binaryArray[i]=='0')||(binaryArray[i]=='1')))
x=0;
break;
}
return(x);
}
//finding the 2's complement logic
void twosComplement(char binaryArray[],char a[])
{
int validate;
/*char a[LENGTH];*/
int l,i;
l=strlen(binaryArray);
for(i=l-1; i>=0; i--)
{
if(binaryArray[i]=='0')
a[i]='1';
else
a[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if(a[i]=='0')
a[i]='1';
else
{
a[i]='0';
validate=1;
}
}
else
{
if((validate==1)&&(a[i]=='0'))
{
a[i]='1';
validate=0;
}
else if((validate==1)&&(a[i]=='1'))
{
a[i]='0';
validate=1;
}
}
}
a[l]='\0';
return;
}
Output:
Example #3
Decimal number 2’s complement
Code:
#include<stdio.h>
#include<stdlib.h>
#define LENGTH 8
//main method for executing the code
int main()
{
//initialize carry variable
int carryDigit = 1;
//initialoze 1's and 2's complement variables
char input[LENGTH + 1], oneComplement[LENGTH + 1], twoComplement[LENGTH + 1];
//declaring integer variables
int a[10],n,i;
//ask the user to enter any decimal number
printf("Enter the number to convert: ");
scanf("%d",&n);
int tempInput=n;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("Binary number of the input is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
input[i] = a[i] + '0';
}
printf("\n");
for( int var= 0; var < LENGTH; var++)
{
if(input[var] == '0')
{
oneComplement[var] = '1';
}
else if(input[var] == '1')
{
oneComplement[var] = '0';
}
}
oneComplement[LENGTH] = '\0';
//2's complement logic within for loop
for(int var = LENGTH - 1; var >= 0; var--)
{
if(oneComplement[var] == '1' && carryDigit == 1)
{
twoComplement[var] = '0';
}
else if(oneComplement[var] == '0' && carryDigit == 1)
{
twoComplement[var] = '1';
carryDigit = 0;
}
else
{
twoComplement[var] = oneComplement[var];//say 1's and 2's complement are same
}
}
twoComplement[LENGTH] = '\0';
//printing 2's complement output
printf("2's complement of 8 digit decimal number %d is=> %s\n",tempInput, twoComplement);//Ignore @ in the output at last digit
return 0;
}
Output:
Conclusion
C language 2s complement is used for blinking the lights and security encoding applications used. So we just add the 1 to the 1’s complement output to get the resultant output for the 2s complement.
Recommended Articles
This is a guide to 2s Complement in C. Here we discuss How Does 2’s Complement Work in C Language along with the examples. You may also have a look at the following articles to learn more –