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.

Hareesh said:   1 decade ago
main() is function first it will call , after that printf will work.

Note : Printf function you can write in inside main function.

Like main(printf("hai"))

If anybody ask Printf function how can write without ends with semicolon means this is solution.
(1)

Rakesh said:   9 years ago
Answer = 0 because,

Binary of -1(in 2 byte) 1111 1111 1111 1111.
Binary of ~0(in 2 byte) 1111 1111 1111 1111.
Now, apply Xor (-1 ^ ~0) = 0000 0000 0000 0000(ans).

Hint:use Xor truth table.
(2)

Xyz said:   8 years ago
Please explain me why \ is used?

Ramesharavind said:   8 years ago
@Vinod.

Your explanation is good, Thanks.

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

Laxman said:   7 years ago
Let you take int i=1;

int is 2-byte range in turbo c, 4 bytes in gcc, now consider turbo c==>2 bytes means 16 bits right so these 16 bits stored in memory for 1 is (0000 0000 0000 0001). Before discoursing about -1; what is 2's complement of x, that is exactly equal to -x. note: negative numbers stored in the memory is 2's comp of a positive number. so we have to find what is 2's comp of 1. 2's comp=1'comp+1;

0000 0000 0000 0001==>1'comp is==>1111 1111 1111 1110 ==>add 1==>1111 1111 1111 1111. So finally the -1 is stored in memory is 1111 1111 1111 1111.

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


Post your comments here:

Your comments will be displayed after verification.