in Programming in C edited by
422 views
1 vote
1 vote

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$
in Programming in C edited by
by
422 views

3 Comments

I am not getting, why 2nd Printf is not giving 7
1
1

That ^ 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

pq means = 0010 ^ 0101 = 0111 = 4+2+1 = 7

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

so output is 7,2 which is option D .

1
1

@aarati mishra

2nd Printf() is not giving 7 that's because p is no more 0010 it have changed to 0111 due to pin first assignment, now that changed p value we use in our 2nd assignment qp .

1
1

1 Answer

2 votes
2 votes
Best answer

^ 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