C program to find twos complement of a binary number

Write a C program to input binary number from user and find twos complement of the binary number. How to find 2s complement of a binary number in C. Logic to find twos complement of a binary number in C programming.

Example

Input

Input binary number: 01101110

Output

Twos complement: 10010010

Required knowledge

Basic C programming, If else, For loop, String

What is twos complement?

Twos complement of an <i>N</i>-bit number is defined as the complement with respect to 2<sup>N</sup>. It is the result of subtracting the number from 2<sup>N</sup>, which in binary is one followed by <i>N</i> zeroes.

In simple words twos complement is defined as sum of ones complement of a binary number and 1.

Twos complement of binary value

Logic to find twos complement

Step by step descriptive logic to find twos complement of a binary string.

  1. Input a binary string from user. Store it in a variable say binary.
  2. Find ones complement of the binary string. Store the result in some variable say onesComp.
  3. Declare and initialize another binary string variable to store twos complement, say twosComp = "".
  4. Initialize a variable to store carry bit during addition, say carry = 1.

    You may think now, why initialize carry to 1, why not 0? Since, we need to add only 1 to the binary string hence, I have initially assumed carry bit as the bit to be added.

  5. Run a loop from length of binary string to 1, decrement 1 in each iteration. The loop structure should look like for(i=SIZE-1; i>=1; i--) (where SIZE is length of the binary string).

    You may think, why iterating in reverse order? Because addition is carried out in right to left order.

  6. Inside the loop there can be three cases.
    • If both current binary bit and carry bit are 1 then, put 0 to twos complement. Which is if(onesComp[i]=='1' && carry==1) then, twosComp[i] = '0', carry is still preserved to 1.
    • If current binary bit is 0 and carry bit is 1 then, put 1 to twos complement and set carry bit to 0. Which is if(onesComp[i]=='0' && carry==1) then, twosComp[i] = '1' and carry = 0.
    • If carry bit is 0, then assign the value of onesComp to twosComp.

Program to find twos complement of a number

/**
 * C program to find twos complement of a binary number
 */

#include <stdio.h>

#define SIZE 8

int main()
{
    char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
    int i, carry=1;

    printf("Enter %d bit binary value: ", SIZE);
    
    /* Input 8-bit binary string */
    gets(binary);

    /* Find ones complement of the binary number */
    for(i=0; i<SIZE; i++)
    {
        if(binary[i] == '1')
        {
            onesComp[i] = '0';
        }
        else if(binary[i] == '0')
        {
            onesComp[i] = '1';
        }
    }
    onesComp[SIZE] = '\0';

    /*
     * Add 1 to the ones complement
     */
    for(i=SIZE-1; i>=0; i--)
    {
        if(onesComp[i] == '1' && carry == 1)
        {
            twosComp[i] = '0';
        }
        else if(onesComp[i] == '0' && carry == 1)
        {
            twosComp[i] = '1';
            carry = 0;
        }
        else
        {
            twosComp[i] = onesComp[i];
        }
    }
    twosComp[SIZE] = '\0';

    printf("Original binary = %s\n", binary);
    printf("Ones complement = %s\n", onesComp);
    printf("Twos complement = %s\n", twosComp);

    return 0;
}

Output

Enter 8 bit binary value: 01101100
Original binary = 01101100
Ones complement = 10010011
Twos complement = 10010100

Happy coding 😉