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 1 of 3.
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).
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.
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.
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;
}
#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;
}
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
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 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.
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)
i.e.
1000 0000 0000 0001 (-1)
1111 1111 1111 1111 (65,535)
Bitwise XORed together:
0111 1111 1111 1110 (32,766)
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).
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).
Nandu said:
1 decade ago
@Nitin you are correct.
Vikas said:
1 decade ago
@Nitin
You are so intelligent.
You are so intelligent.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers