C Program To Convert Binary Number To 2’s Complement

A 2’s complement is used for binary operations in a computer system. In this example program, you are going to receive a binary string and convert the string into 2’s complement in two steps.

Learn the basics of C programming before you begin with the example. Skip this step if you already familiar with these concepts.

Problem Definition

The program ask for a input binary string. To convert this binary string in to 2’s complement, you need two steps.

  1. Convert the input binary string to 1’s complement.
  2. Add 1 to the 1’s complement obtained.

For example, suppose a the input string is 1100.

Step 1: The 1’s complement can be obtained by converting 1’s in to 0 and 0s into 1s.

1100 becomes 0011

Step 2: Add 1 to the 1’s complement to get 2’s complement of the number.

This step requires you to understand the binary math.

1 + 1 = 10

1 + 0 = 1

0 + 1 = 1

0 + 0 = 0

Using the above, you add 1 to the 1’s complement.

0011

 + 1      
__________
0100
__________

Therefore,

The two’s complement of the number is 0100.

Flowchart

Flowchart - Program for 2's Complement
Flowchart – Program for 2’s Complement

Program Source Code

#include <stdio.h>
#include <stdlib.h>
void complement(char *a);
int main()
{
    char a[16];
    int i;
    printf("Enter the binary number:");
    gets(a);
for(i=0;a[i] != '
#include <stdio.h>
#include <stdlib.h>
void complement(char *a);
int main()
{
char a[16];
int i;
printf("Enter the binary number:");
gets(a);
for(i=0;a[i] != '\0';i++)
{
if(a[i] != '0' && a[i]!= '1')
{
printf("The number entered is not a binary number"); 
printf("Enter the correct number!");
exit(0);
}
}
complement(a);
getch();
system("PAUSE"); 
return 0;
}
void complement (char *a)
{
int l, i, c = 0;
char b[16];
l = strlen(a);
/* Get the 1's complement of the given binary string */
for(i = l-1;i>=0;i--)
{
if(a[i] == '0')
{
b[i]= '1';
}
else
{
b[i] = '0';
}
}
/* Add 1 to the 1's complement obtained in the previous step */
for(i = l-1;i>=0;i--)
{
if(i == l-1)
{
if(b[i]== '0')
{
b[i] = '1';
}
else
{
b[i] == '0';
c = 1;
}
}
else
{
if(c==1 && b[i] == '0')
{
b[i] = '1';
c = 0;
}
else if(c == 1 && b[i] == '1')
{
b[i] = '0';
c = 1;
}
}
}
b[l] = '\0';
printf("The 2's Complement is %s\n", b);
}
';i++) { if(a[i] != '0' && a[i]!= '1') { printf("The number entered is not a binary number"); printf("Enter the correct number!"); exit(0); } } complement(a); getch(); system("PAUSE"); return 0; } void complement (char *a) { int l, i, c = 0; char b[16]; l = strlen(a); /* Get the 1's complement of the given binary string */ for(i = l-1;i>=0;i--) { if(a[i] == '0') { b[i]= '1'; } else { b[i] = '0'; } } /* Add 1 to the 1's complement obtained in the previous step */ for(i = l-1;i>=0;i--) { if(i == l-1) { if(b[i]== '0') { b[i] = '1'; } else { b[i] == '0'; c = 1; } } else { if(c==1 && b[i] == '0') { b[i] = '1'; c = 0; } else if(c == 1 && b[i] == '1') { b[i] = '0'; c = 1; } } } b[l] = '
#include <stdio.h>
#include <stdlib.h>
void complement(char *a);
int main()
{
char a[16];
int i;
printf("Enter the binary number:");
gets(a);
for(i=0;a[i] != '\0';i++)
{
if(a[i] != '0' && a[i]!= '1')
{
printf("The number entered is not a binary number"); 
printf("Enter the correct number!");
exit(0);
}
}
complement(a);
getch();
system("PAUSE"); 
return 0;
}
void complement (char *a)
{
int l, i, c = 0;
char b[16];
l = strlen(a);
/* Get the 1's complement of the given binary string */
for(i = l-1;i>=0;i--)
{
if(a[i] == '0')
{
b[i]= '1';
}
else
{
b[i] = '0';
}
}
/* Add 1 to the 1's complement obtained in the previous step */
for(i = l-1;i>=0;i--)
{
if(i == l-1)
{
if(b[i]== '0')
{
b[i] = '1';
}
else
{
b[i] == '0';
c = 1;
}
}
else
{
if(c==1 && b[i] == '0')
{
b[i] = '1';
c = 0;
}
else if(c == 1 && b[i] == '1')
{
b[i] = '0';
c = 1;
}
}
}
b[l] = '\0';
printf("The 2's Complement is %s\n", b);
}
'; printf("The 2's Complement is %s\n", b); }

Output

Enter the binary number:11000101
The 2's Complement is 00111011

The given input string here is 11000101 and the final output is its 2’s complement.

11000101  has 1’s complement of 00111010

And if you add 1 to 1’s complement.

00111010 + 1 => 00111011

Therefore, the 2’s complement is 00111011.

error

Enjoy this blog? Please spread the word :)