2s Complement in C

2s Complement in C


There are two types of complement of Binary number: 1’s complement and 2’s complement. To get 1’s complement of a binary number, simply invert the given number. For example, 1’s complement of binary number 110010 is 001101. To get 2’s complement of binary number is 1’s complement of given number plus 1 to the least significant bit (LSB). For example 2’s complement of binary number 10010 is (01101) + 1 = 01110.

Example 1 : Find 2’s complement of binary number 10101110.

Forst invert each bit of given binary number, which will be 01010001. Then add 1 to the LSB of this result, i.e., 01010001+1=01010010.

Example 2 : Find 2’s complement of binary number 10001.001.

First invert each bit of given binary number, which will be 01110.110 Then add 1 to the LSB of this result, i.e., 01110.110+1=01110.111.

Program :