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.

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

Suman said:   1 decade ago
What are those forward slashes in the program doing?

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.

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)

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.

Nandu said:   1 decade ago
@Nitin you are correct.


Post your comments here:

Your comments will be displayed after verification.