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.

**Example**

**Input**

Input binary number: 01101110

**Output**

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

-bit number is defined as the complement with respect to *N*`2`

. It is the result of subtracting the number from ^{N}`2`

, which in binary is one followed by ^{N}

zeroes.
*N*

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

## Logic to find twos complement

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

- Read a binary string from user. Store it in some variable say binary.
- Convert the binary string to ones complement. Store it in some variable say onesComp.
- Initialize another binary string variable to store twos complement, say twosComp = "".
- 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.
- 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.
- 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 gets(binary); /* * 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; } 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; }

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

Happy coding ;)

### Recommended posts

- Loop programming exercises and solutions in C.
- C program to convert Binary to Octal number system.
- C program to convert Binary to Decimal number system.
- C program to convert Octal to Binary number system.
- C program to convert Decimal to Binary number system.
- C program to convert Hexadecimal to Binary number system.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)