# Twos Complement of a Binary Number in C

We know how to represent 2 in binary it’s simple haa! but do you know how to represent -2 in binary? There are basically three methods to represent a negative number in binary.

• Signed Bit representation : In this method we take the MSB bit as a signed bit if MSB is 1 number is negative if it is 0 number will be positive
• One’s Complement : In this type each bit is converted into oppposite bit for example 1010 will be converted to 0101
• Two’s Complement : In this method we first find out the one’s complement of the binary number then we add 1 to the resultant binary number for example 1010
one’s complemet — 0101
Now add 1 to the above one’s complement the resultant number will be 0110 which is the two’s complement of 1010

C program for adding two Binary Numbers

Now we know how to find out the two’s complement of a binary number, now lets dive straight into the programming part, Let’s C the code .

Before going through the code we suggest you to read our previous post C program for adding two Binary Numbers because we are going to use most of functions present in that program.

The only new function we are going to make is two_comp() which will find the two’s complement of the binary number

See the below code for tow_comp()

``int two_comp(int x[]){   int i = 0;   for(i=0;i<COUNT;i++){       if(x[i]==1){           x[i] = 0;       }       else{           x[i] = 1;       }   }   add_bin(x,q); }``

You can find the add_bin() function in our C program for adding two Binary Numbers post

Now let us C the full program for finding the two’s complement of a binary number

``#include<stdio.h> #define COUNT 4 //change count to change the number of bits int sum[COUNT]; int q[COUNT] = {0,0,0,1}; int conv_bin(int bin); int take_bin(int x[]); int print_bin(int x[]); int add_bin(int x[],int y[]); int two_comp(int x[]); int main(){ int a[COUNT] = {0,0,0,0}; int m[COUNT]; int sum[COUNT]; printf("Enter a 4-bit Binary number(space between each bit): "); take_bin(m);   printf("The two's complement of the above binary number is : '");   two_comp(m); return 0; } int take_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ scanf("\%d",&x[i]); }  } int print_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ printf("\%d",x[i]); } printf("");  } int add_bin(int x[],int y[]){ int i,carry=0; for(i = COUNT-1;i>=0;i--){ if(x[i]==0 && y[i]==0 && carry==0){ sum[i] = 0; carry = 0; } else if(x[i]==0 && y[i]==0 && carry==1){                       sum[i] = 1;                       carry = 0;               } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==0){ sum[i] = 1; carry = 0; } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==1){                       sum[i] = 0;                       carry = 1;               } else if(x[i]==1 && y[i]==1 && carry==0){ sum[i] = 0; carry = 1; } else if(x[i]==1 && y[i]==1 && carry==1){                       sum[i] = 1;                       carry = 1;               } } print_bin(sum); } int two_comp(int x[]){   int i = 0;   for(i=0;i<COUNT;i++){       if(x[i]==1){           x[i] = 0;       }       else{           x[i] = 1;       }   }   add_bin(x,q); }``

Output Also, don’t forget to subscribe to our Newsletter.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: