Machine-translated by Google. Questions are authored in English.
Top

Discussion

Predict the output of following program.

#include <stdio.h>

int main()
{
 int a=10;
 int b=2;
 int c;
 
 c=(a & b);
 printf("c= %d",c);
 
 return 0;
}

 

  • A.c= 12
  • B.c= 10
  • C.c= 2
  • D.c= 0

Answer: C

Correct answer: c= 2

Write both operands in binary and AND them position by position:

\[ 10 = 1010_2 \qquad 2 = 0010_2 \]

A result bit is 1 only where both inputs are 1, which happens in the second position alone, giving \( 0010_2 = 2 \).

The distractor 12 is what | would give and 0 is what ^ would give in the other direction - useful checks, since 10 and 2 share exactly one bit. Note that && would have given 1, both operands being non-zero.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.