C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
Discussion:
27 comments Page 2 of 3.
Vinod Kr. Rai said:
1 decade ago
Calculate: -1^~0
-1 => 1111 1111 1111 1111
~0 => 1111 1111 1111 1111
--------------------------------
-1^~0 => 0000 0000 0000 0000 => is binary of 0 (zero).
--------------------------------
Firstly find the binary of -1:
1 => 0000 0000 0000 0001
1's compliment of 0000 0000 0000 0001 is:
All 0 is converted into 1 and all 1 into 0, now
1111 1111 1111 1110
Now 2's complement of 1111 1111 1111 1110 is:
Add 1 into 1's complement
1111 1111 1111 1110
+ 0000 0000 0000 0001
----------------------------
1111 1111 1111 1111 => is binary of -1
----------------------------
Now, binary of 0 is:
0 => 0000 0000 0000 0000 => is binary of 0
~0 => to convert all 0 into 1 and all 1 into 0, i.e.
1111 1111 1111 1111 => is binary of ~0
-----------------------------------
^ is called Bitwise XOR operator
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
------------------------------------
-1 => 1111 1111 1111 1111
~0 => 1111 1111 1111 1111
--------------------------------
-1^~0 => 0000 0000 0000 0000 => is binary of 0 (zero).
--------------------------------
Firstly find the binary of -1:
1 => 0000 0000 0000 0001
1's compliment of 0000 0000 0000 0001 is:
All 0 is converted into 1 and all 1 into 0, now
1111 1111 1111 1110
Now 2's complement of 1111 1111 1111 1110 is:
Add 1 into 1's complement
1111 1111 1111 1110
+ 0000 0000 0000 0001
----------------------------
1111 1111 1111 1111 => is binary of -1
----------------------------
Now, binary of 0 is:
0 => 0000 0000 0000 0000 => is binary of 0
~0 => to convert all 0 into 1 and all 1 into 0, i.e.
1111 1111 1111 1111 => is binary of ~0
-----------------------------------
^ is called Bitwise XOR operator
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
------------------------------------
(1)
Jessie said:
1 decade ago
@Nitin is correct.
Saiprabha said:
1 decade ago
-1 means it will treated as 2's complement
that means adding 1 to the result of 1's complement.
Binary form of 1 is :
0000 0000 0000 0001
1's :---- 1111 1111 1111 1110
adding 1 then
:----- 1111 1111 1111 1111
and ^ means it will print 0 if both are same(1,1---0)(0,0----0)
otherwise 1 (1,0 -------1)
0's is :-- 1111 1111 1111 1111
now for -1 **** 1111 1111 1111 1111
for ~0 **** 1111 1111 1111 1111
now we will perform the ^ operaiton
1111 1111 1111 1111
1111 1111 1111 1111
-------------------------------------
0000 0000 0000 0000
so the answer is zero
that means adding 1 to the result of 1's complement.
Binary form of 1 is :
0000 0000 0000 0001
1's :---- 1111 1111 1111 1110
adding 1 then
:----- 1111 1111 1111 1111
and ^ means it will print 0 if both are same(1,1---0)(0,0----0)
otherwise 1 (1,0 -------1)
0's is :-- 1111 1111 1111 1111
now for -1 **** 1111 1111 1111 1111
for ~0 **** 1111 1111 1111 1111
now we will perform the ^ operaiton
1111 1111 1111 1111
1111 1111 1111 1111
-------------------------------------
0000 0000 0000 0000
so the answer is zero
Suman said:
1 decade ago
What are those forward slashes in the program doing?
Ishan said:
1 decade ago
Most of the part is correctly explained.
Except ~ is 1s complement not negation
Negation of 0 is negation is logical operation
1s complement is bitwise operation
Negation of 0 will yield 1
But
~0= 1111 1111 1111 1111
Except ~ is 1s complement not negation
Negation of 0 is negation is logical operation
1s complement is bitwise operation
Negation of 0 will yield 1
But
~0= 1111 1111 1111 1111
Ishan said:
1 decade ago
\ split the macros into multiple line
For example
#define P(int i) { \
printf("%d",i);\
}
Or
#define P(int i) { printf("%d",i);}
For example
#define P(int i) { \
printf("%d",i);\
}
Or
#define P(int i) { printf("%d",i);}
Shiv said:
1 decade ago
I'm very confuse, what and how exactly program is executing please explain compilation and execution phase?
Abcd said:
1 decade ago
Please explain multiple macros how to split?
Madhav said:
1 decade ago
BY using \ we can use multiple line macro, it indicates that first line is continued.
Yogesh kumar said:
1 decade ago
Can anyone ans me how the execution is reaching to PRINTF block as main is defined after it?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers