edited by
420 views
1 votes
1 votes

Read the following program fragment:       

#include<stdio.h>
int main()
{
int p = 2, q = 5;
p = p^q;
q = q^p;
printf("%d%d",p,q);
return 0;
}

The output is:

  1. $5 \ 2$
  2. $2 \ 5$
  3. $7 \ 7$
  4. $7 \ 2$
edited by

1 Answer

Best answer
2 votes
2 votes

^ is known as " Bitwise Exclusive OR Operator " .

The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand.

 If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

p = 2 = 0010

q = 5 = 0101

pmeans = 0010 ^ 0101 = 0111 = 4+2+1 = 7

qp means = 0101 ^ 0111 ( because now p is changed to 0111) =    0010 = 2

Hence  output is 7,2 which is option D .

selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
421 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
365 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
221 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
220 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”...