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)
1
0
-1
2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
27 comments Page 2 of 3.

Madhav said:   1 decade ago
BY using \ we can use multiple line macro, it indicates that first line is continued.

Abcd said:   1 decade ago
Please explain multiple macros how to split?

Shiv said:   1 decade ago
I'm very confuse, what and how exactly program is executing please explain compilation and execution phase?

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);}

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

Ankit Anand said:   2 decades ago
10000000 00000000 00000000 00000001
11111111 11111111 11111111 11111111 (after negation or inverting)

for 0^0 = 0
1^1 = 0

So the answer is 0 (zero).

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

Jessie said:   1 decade ago
@Nitin is correct.

Vikas said:   1 decade ago
If an integer on this machine is 2-bytes, why is the answer not 32,766?
0000 0000 0000 0001=1
1111 1111 1111 1110=-1 1's compliment of -1

1111 1111 1111 1111=-1 in 2's compliment
^1111 1111 1111 1111=0 after negation or inverting
........................
0000 0000 0000 0000
so therefore we know that 0^0=0
1^1=0
so the answer is 0(Zero).
thanks frds.
all the best.

Vikas said:   1 decade ago
@Nitin

You are so intelligent.


Post your comments here:

Your comments will be displayed after verification.