ProgrammingModerateInfosys · Wipro pattern

What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift; precedence as in C: << binds tighter than &, which binds tighter than ^, then |.)

What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift; precedence as in C: << binds tighter than &, which binds tighter than ^, then |.)
Integer a = 12, b = 10
Print (a & b) + (a | b), a ^ b << 1
  1. A. 22 24
  2. B. 22 12
  3. C. 14 24
  4. D. 22 6

Correct answer: A. 22 24

12 = 1100, 10 = 1010: AND = 1000 = 8, OR = 1110 = 14, sum 22 (always equal to a + b). In a ^ b << 1 the shift happens first: b << 1 = 20 = 10100, and 01100 ^ 10100 = 11000 = 24. Doing the XOR first gives (12 ^ 10) << 1 = 6 << 1 = 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic