3 3 votes What will be the output of the program ? #include<stdio.h> int main() { int i=4, j=8; printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j); return 0; } A. 12, 12, 12 B. 112, 1, 12 C. 32, 1, 12 D. -64, 1, 12 Programming in C + – Anil Khatri 4.9k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Anil Khatri commented Sep 8, 2016 reply Follow flag i got first 2 but my doubt is in 3rd why we do bitwise xor both are in integer is it 4^6=4096 na 0 0 replyShare Sanjay Sharma commented Sep 8, 2016 reply Follow flag no it is not power operator it is bitwise xor (^) operator for power we use mathematical power operator in math.h 2 2 replyShare Please log in or register to add a comment.
Best answer 4 4 votes Your Answer needs operator precedence table of C given http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm so your code can be more specifically written as printf("%d,%d,%d",i|(j&j)|i,i|(j&j)|i,i^j); (j&j)=1000 AND 1000=1000=j(taking only lower 4 bits) now the code become $i\mid j\mid i\Rightarrow \left ( i\mid j \right )\mid i\Rightarrow 1100\mid 0100= 1100$ (as "| " is left associative) also $i\Lambda j\Rightarrow (1000 \Theta 0100)= 1100$(XOR) so answer is 1100,1100,1100=(12,12,12) A) sourav. answered Sep 8, 2016 • edited Sep 13, 2016 by sourav. sourav. comment Share Follow See all 7 Comments 7 7 Comments reply Anil Khatri commented Sep 8, 2016 reply Follow flag but why u consider ^ as bitwise xor not exponent? 0 0 replyShare sourav. commented Sep 8, 2016 i edited by sourav. Sep 8, 2016 reply Follow flag check the operator table ,the link i have given. Who told that "^" operator=power in C,it is always bitwise XOR, power in C is represented by pow(base,power) "^" operator=power is in ENGLISH not in C 4 4 replyShare ankit commented Sep 13, 2016 reply Follow flag @Sourav Sir , but in "now the code become i∣j∣i⇒(i∣j)∣i⇒1100∣1000=1100 (as "| " is left associative)" here in last bitwise OR operation, you wrote the value of 'j' instead of 'i....here i=4=0100 ... please correct me if I'm wrong 0 0 replyShare sourav. commented Sep 13, 2016 reply Follow flag Question-:i|(j&j)|i step 1:j&j=j step2:i | j | i step 3:( i | j )| i step 4:1100|1000=1100. Now say where is your doubt? 0 0 replyShare ManojK commented Sep 13, 2016 reply Follow flag step 3:( i | j )| i step 4:1100|0100=1100. 2 2 replyShare sourav. commented Sep 13, 2016 reply Follow flag updated ..!!! thank you @manoj and @ankit.@ankit don't call me sir ,as i am also aspirant like you :) 1 1 replyShare ankit commented Sep 13, 2016 reply Follow flag ok :) 0 0 replyShare Please log in or register to add a comment.
2 2 votes ans will be A 12,12,12, | bitwise or & bitwise and ^ bitwise xor now i=4 in bits 100 , j=8 in bits 1000 j&j =1000 or 8 so i | j&j= 4|8=12|4=12 so first 2 values will be 12 and 12 i^j=0100 xor 1000 =1100 so it is also 12 Sanjay Sharma answered Sep 8, 2016 Sanjay Sharma comment Share Follow See 1 comment 1 1 comment reply Anil Khatri commented Sep 8, 2016 reply Follow flag but why u consider ^ as bitwise xor not exponent? 0 0 replyShare Please log in or register to add a comment.