C program to find twos complement of a binary number

Previous Program Next Program

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



Input binary number: 01101110


Twos complement: 10010010

Required knowledge

Basic C programming, If else, For loop, String

Must have programming knowledge for this program.

What is twos complement?

Wikipedia states, twos complement of an N-bit number is defined as the complement with respect to 2N. It is the result of subtracting the number from 2N, which in binary is one followed by N zeroes.

In simple words twos complement can be defined as sum of 1's complement of the binary number and 1.

Twos complement of a binary number

Logic to find twos complement

Below is the step by step descriptive logic to find twos complement of a binary string.

  1. Read a binary string from user. Store it in some variable say binary.
  2. Convert the binary string to ones complement. Store it in some variable say onesComp.
  3. 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, decrementing 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:
    Case I: If both current binary bit and carry bit are 1 then, put 0 to twos complement. Which is ifonesComp[i]=='1' && carry==1 then, twosComp[i] = '0', carry is still preserved to 1.
    Case II: 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.
    Case III: If carry bit is 0, then assign the value of onesComp to twosComp.

Program to find twos complement

 * C program to find 2's 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);
    // Read 8-bit binary string

     * Finds the 1's 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 1's 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;
            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;
Enter 8 bit binary value: 01101100
Original binary = 01101100
Ones complement = 10010011
Twos complement = 10010100

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment