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 3 of 3.

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

Nitin said:   1 decade ago
00000000 00000000 00000000 00000001 = 1
11111111 11111111 11111111 11111110 = -1 in 1's compliment
we now have to add 1 to make it into 2's compliment .
So,

11111111 11111111 11111111 11111111 = -1 in 2's compliment

11111111 11111111 11111111 11111111 (0 after negation or inverting)

for 0^0 = 0
1^1 = 0

So the answer is 0 (zero).

Dough said:   1 decade ago
If an integer on this machine is 2-bytes, why is the answer not 32,766?

i.e.
1000 0000 0000 0001 (-1)
1111 1111 1111 1111 (65,535)

Bitwise XORed together:
0111 1111 1111 1110 (32,766)

Madhureddy said:   1 decade ago
@Naveen

Ankit is correct only, because C is a compiler dependent language, int occupies 2 bytes in Turbo C compiler, where as GCC is conncern it allots 4-bytes to integer.

Naveen said:   1 decade ago
Ankit anand your answer is correct. But one small mistake. you assign integer as 4 bytes but integer only having 2 bytes

Sree said:   1 decade ago
After the First macro function executed

#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(printf("%d\n", -1^~0);)

After the second Macro function executed

#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}

int main()
{
printf("%d\n", -1^~0);
return 0;
}

Kumar said:   2 decades ago
Hi Ankit Anand,

Thank you very much for your kind explanation.

Can you please explain, how the program will look after preprocessing (Phase I of compilation) ?

Thanks in advance.


Post your comments here:

Your comments will be displayed after verification.