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

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

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)

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)

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)

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

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.

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

Ramesharavind said:   8 years ago
@Vinod.

Your explanation is good, Thanks.

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

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


Post your comments here:

Your comments will be displayed after verification.