C Program to Find Twos Complement of a Binary Number

  • Write a C program to read a binary number and find its two’s complement.
  • Wap in C to find two complement of a binary number.

Required Knowledge

Algorithm to find twos complement of a binary number
1. To find the twos complement of a number first find the ones complement by toggling the bits of the number. Change all 1's to 0's and all 0's to 1's.
2. Add binary 1 to the ones complement to get twos complement.

For Example :
Binary Number = 00101011
Ones Complement = 11010100
Twos Complement = 11010101

C program to find two complement of a binary number

C program to find two complement of a binary number

#include <stdio.h>  
#include <string.h>  
   
int main() {
    char binaryNumber[100], onesComplement[100], twosComplement[100];
    int counter, error=0, digitCount, carry = 1;   
    /*
     * Take a binary string as input from user
     */
    printf("Enter a Binary Number\n");  
    scanf("%s", binaryNumber);  
   
    /* 
     * To get one's complement, we toggle 
     * 1's to 0's and 0's to 1's
     */
    digitCount = strlen(binaryNumber);
     
 for(counter=0; counter < digitCount; counter++) {  
        if(binaryNumber[counter]=='1') {  
            onesComplement[counter] = '0';  
        } else if(binaryNumber[counter]=='0') {  
            onesComplement[counter] = '1';  
        } else {  
            printf("Error :( ");  
            return 1;
        }  
    }  
    onesComplement[digitCount] = '\0';
     
    /* 
     * Add 1 to the 1's complement. Perform binary addition
     */ 
    for(counter = digitCount-1; counter >= 0; counter--) {  
        if(onesComplement[counter]=='1' && carry==1){
            twosComplement[counter] = '0';  
        } else if(onesComplement[counter]=='0' && carry==1) {  
            twosComplement[counter] = '1';  
            carry = 0;  
        } else {  
            twosComplement[counter] = onesComplement[counter];  
        }  
    }  
    twosComplement[digitCount] = '\0'; 
       
    printf("Two's Complement : %s", twosComplement);  
   
    return 0;  
}

Output

Enter a Binary Number
11100001
Two's Complement : 00011111