2s Complement in C
0 476
The two's complement representation in C allows for the representation of both positive and negative numbers using binary code, achieving this by flipping the bits and adding one to the binary representation of the negative numbers.
To get the 2's Complement of number, you first flip all the bits which changes from (0s to 1s and 1s to 0s), and then add 1 to output.
For positive number the binary representation remains same as the positive binary number.
For negative number start with the binary of its positive version.
Then change all (0s to 1s and 1s to 0s) and add 1 to the output.
Process of finding 2's Complement in C
For example:
The decimal number of 5 in binary is (0000 0101).
To represent -5 in 2's complement .
start with binary of its positive version 5: 0000 0101.
Flip all the bits: 1111 1010.
add 1 to the output: 1111 1011.
-5 in 2's complement is 1111 1011.
Program to Find 2's Complement in C
#include<stdio.h> int main() { int A = 10; int twos_complement = ~A+1; printf(" 2's complement of %d is: %d\n", A, twos_complement); return 0; }Output:
2's complement of10 is: -10
Share:
Comments
Waiting for your comments