EX: write a C program to perform Compliment operation of two intiger's. How to write a C program to perform Compliment operation of two intiger's. C program to perform Compliment operation of two intiger's.
Input from user:
a=25;
Expected Output:
After performing Compliment operation the output is: 230
First you need to know how Compliment operator works.
Input from user:
a=25;
Expected Output:
After performing Compliment operation the output is: 230
Logic to perform Compliment operation of two intiger's:
First you need to know how Compliment operator works.
- It sets bit value 1 only if corresponding bit of operand is 0. Means it changes 1 value to 0 and 0 value to 1.
1. We take one intiger numbers a=25.
2. Binary representation of a -Compliment is-
a = 0001 1001
------------------------------------
a~ = 1110 0110 =230 in decimal.
3. After performing Compliment (~), 8 bit binary representation is 1110 0110 which is 230 in decimal.
But output is not 230. The output of the Program is -26.
To understand this you need to know how work's 2's compliment.
- 2's compliment:
Means 2's compliment of a number is equal to the compliment of that number plus 1.
For Example:
We take 230. 8 bit binary representation of 230 is 1110 0110 so 2's compliment of that number is... -(0001 1001)
-0001 1001
+ 1
--------------------
-0001 1010=-26(in decimal)
The bitwise Complement of 25 is 230 in decimal. The 2's compliment of 230 is -26. Hence, the output is -26 not 230.
C program using bitwise compliment:
void main()
{
int a=25;
printf("Output is:%d",~a);
}
No comments:
Post a Comment
If you have any doubts, please discuss here...👇