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.

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)

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.

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.

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).

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

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

Yogesh kumar said:   1 decade ago
Can anyone ans me how the execution is reaching to PRINTF block as main is defined after it?

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

NJ Nath said:   5 years ago
^ is Bitwise XOR operator, perform -1 XOR -1, the answer will be 0000.
(3)

Yasaswini said:   8 years ago
Can anyone give the representation of 1 and -1 in binary digits?


Post your comments here:

Your comments will be displayed after verification.