Bitwise Operators Questions with Answers

80 practice questions on Bitwise Operators (Pseudocode & Code Tracing) with answers and step-by-step solutions. Try 10 samples below, then practise all 80 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output of the following pseudocode?

Integer x
Set x = 13
Print x << 2, x >> 1

Note: << is left shift, >> is right shift. Printed values are separated by spaces.

  1. 52 6
  2. 53 6
  3. 52 7
  4. 52 3

Answer: A. 52 6

Explanation

x << 2 multiplies by 4 and x >> 1 halves (discarding the remainder): 52 6.

Step-by-step solution

  1. X << 2 shifts 13 left by two places, which multiplies by 4: 13 × 4 = 52.
  2. X >> 1 shifts 13 right by one place, which divides by 2 and drops the remainder: 6.
  3. Output is 52 6, which is option A.

Why the other options are wrong

  • B. 13 × 4 = 52, not 53.
  • C. 13 shifted right by one gives 6, not 7.
  • D. Shifting right once gives 6; 3 would need two right shifts.

Tip: Left shift by k multiplies by 2^k; right shift by k divides by 2^k and drops the remainder.

Question 2Easy

Predict the output of the following pseudocode.

Integer x
Set x = 40
Print x << 2, x >> 1

Note: << is left shift, >> is right shift. Printed values are separated by spaces.

  1. 161 20
  2. 160 10
  3. 160 20
  4. 160 21

Answer: C. 160 20

Explanation

x << 2 multiplies by 4 and x >> 1 halves (discarding the remainder): 160 20.

Step-by-step solution

  1. X << 2 is 40 * 4 = 160.
  2. X >> 1 is 40 / 2 = 20.
  3. Printed values are 160 and 20.
  4. Final output is 160 20.

Why the other options are wrong

  • A. 161 20 is wrong; 40 shifted left by two is 160, with no extra 1.
  • B. 160 10 is wrong; 40 shifted right by one is 20, not 10.
  • D. 160 21 is wrong; 40 shifted right by one is 20, not 21.

Tip: Remember left shift multiplies, right shift divides (integer).

Question 3Easy

What will be printed by the following pseudocode?

Integer x
Set x = 7
Print x | 1, x & 1

Note: & is bitwise AND, | is bitwise OR. Printed values are separated by spaces.

  1. 1 1
  2. 7 2
  3. 7 1
  4. 7 7

Answer: C. 7 1

Explanation

x | 1 sets the lowest bit and x & 1 reads it: 7 1.

Step-by-step solution

  1. x = 7, which is 111 in binary.
  2. X | 1 keeps 7, since its last bit is already 1.
  3. X & 1 is 1, because 7 is odd.
  4. The output is 7 1, the correct answer.

Why the other options are wrong

  • A. 1 is wrong for the first value; 7 | 1 is 7.
  • B. 2 is wrong for the second value; 7 & 1 is 1.
  • D. 7 is wrong for the second value; 7 & 1 is 1, not 7.

Tip: X | 1 makes a number odd, and x & 1 tells you whether a number is odd.

Question 4Easy

What will be the output of the following pseudocode?

Integer a, b
Set a = 12, b = 10
Print a & b, a | b, a ^ b

Note: & is bitwise AND, | is bitwise OR, ^ is bitwise XOR. Printed values are separated by spaces.

  1. 14 14 6
  2. 8 14 7
  3. 8 8 6
  4. 8 14 6

Answer: D. 8 14 6

Explanation

Write both numbers in binary and combine bit by bit: AND, OR and XOR give 8 14 6.

Step-by-step solution

  1. 12 in binary is 1100 and 10 is 1010.
  2. AND keeps bits set in both: 1000 = 8.
  3. OR keeps bits set in either: 1110 = 14.
  4. XOR keeps bits set in exactly one: 0110 = 6, so the output is 8 14 6, option D.

Why the other options are wrong

  • A. AND is 8, not 14.
  • B. XOR is 6, not 7.
  • C. OR is 14, not 8.

Tip: AND gives the shared 1s, OR gives all 1s, XOR gives the 1s that differ.

Question 5Medium

Predict the output of the following pseudocode.

Integer x, y
Set x = 100
y = x | (x >> 1)
y = y | (y >> 2)
y = y | (y >> 4)
Print y, y + 1

Note: | is bitwise OR, >> is right shift. Printed values are separated by spaces.

  1. 127 128
  2. 128 128
  3. 42 43
  4. 127 129

Answer: A. 127 128

Explanation

Smearing the highest set bit to the right fills all lower bits, so y = 2^k - 1 and y + 1 is the next power of two: 127 128.

Step-by-step solution

  1. 100 is 1100100 in binary.
  2. The OR steps fill every bit below the top 1 bit, so y becomes 1111111, which is 127.
  3. y + 1 is 128. The output is 127 128: option A.

Why the other options are wrong

  • B. The first value is 127, not 128, because the fill gives all 1s below the top bit.
  • C. 42 and 43 are not produced; the OR steps give 127 and 128.
  • D. The second value is 128, not 129, because y + 1 is 128.

Tip: The fill gives 2^k - 1 for the top bit at position k - 1, and adding 1 gives the next power of two.

Question 6Medium

Predict the output of the following pseudocode.

Integer x, y
Set x = 91
y = x & 15
x = x >> 4
Print x, y

Note: & is bitwise AND, >> is right shift. Printed values are separated by spaces.

  1. 5 11
  2. 6 11
  3. 5 12
  4. 5 95

Answer: A. 5 11

Explanation

x & 15 keeps the low 4 bits and x >> 4 the rest, splitting 91 into its two hexadecimal digits: 5 11.

Step-by-step solution

  1. y = 91 & 15 keeps only the last 4 bits of 91.
  2. 91 = 80 + 11, and 80 is 5 × 16, so the last 4 bits give y = 11.
  3. x = 91 right shifted by 4 is 91 ÷ 16 = 5.69, with the fraction dropped, so x = 5.
  4. The output is 5 11, the correct answer.

Why the other options are wrong

  • B. 6 is wrong; 91 ÷ 16 is 5.69, and the whole part is 5.
  • C. 12 is wrong for the second value; the last 4 bits of 91 give 11.
  • D. 95 is wrong; y keeps only the last 4 bits, which is 11.

Tip: A right shift by 4 divides by 16, and AND with 15 keeps the remainder after dividing by 16.

Question 7Medium

Predict the output of the following pseudocode.

Integer x, i, c
Set x = 15, c = 0
for (each i from 0 to 7)
    if ((x & (1 << i)) == 0)
        c = c + 1
    End if
End for
Print c

Note: & is bitwise AND, << is left shift.

  1. 3
  2. 4
  3. 5
  4. 0

Answer: B. 4

Explanation

c counts the 0 bits among the low 8 bits of 15: 4.

Step-by-step solution

  1. 15 in binary is 1111.
  2. Over the low 8 bits, 15 is 00001111, so the top four bits are all 0.
  3. There are four zero bits, so c = 4, the correct answer.

Why the other options are wrong

  • A. 3 is wrong; the top four bits of the 8-bit form are zeros.
  • C. 5 is wrong; only four bits are zero.
  • D. 0 is wrong; 15 has four zero bits in its low 8 bits.

Tip: Count zeros over a fixed width, such as 8 bits. A small number has many leading zeros.

Question 8Medium

What will be the output of the following pseudocode?

Integer x, c
Set x = 183, c = 0
while (x > 0)
    c = c + (x & 1)
    x = x >> 1
End while
Print c

Note: & is bitwise AND, >> is right shift.

  1. 6
  2. 3
  3. 362
  4. 7

Answer: A. 6

Explanation

The loop tests the lowest bit and shifts right, counting the 1 bits of the starting value: 6.

Step-by-step solution

  1. The loop adds the lowest bit of x to c and shifts x right, so c counts the 1 bits of 183.
  2. 183 = 128 + 32 + 16 + 4 + 2 + 1, which is binary 10110111.
  3. The 1 bits are 1, 2, 4, 16, 32 and 128, six in total.
  4. Output is 6, which is option A.

Why the other options are wrong

  • B. 183 has six 1 bits, not three.
  • C. The loop counts bits, so the result is at most 8 here, not 362.
  • D. 183 has six 1 bits, not seven.

Tip: This loop counts set bits (popcount); write the number in binary and count the 1s.

Question 9Hard

What will be printed by the following pseudocode?

Integer a[6] = {5, 5, 6, 8, 6, 2}
Integer i, x
Set x = 0
for (each i from 0 to 5)
    x = x ^ a[i]
End for
Print x

Note: ^ is bitwise XOR. Array indices start at 0.

  1. 11
  2. 8
  3. 15
  4. 10

Answer: D. 10

Explanation

Equal values cancel under XOR, so the result is the XOR of the values that appear an odd number of times: 10.

Step-by-step solution

  1. XOR of equal values is 0, so 5^5 = 0.
  2. 0^6 = 6; 6^8 = 14; 14^6 = 8; 8^2 = 10.
  3. Result is 10.

Why the other options are wrong

  • A. 11 is not reached by any partial XOR here.
  • B. Forgets to XOR in the final 2.
  • C. 15 would need different values.

Tip: Cancel duplicates first: only 6,6 cancel too, leaving 8^2 = 10.

Question 10Hard

What will be printed by the following pseudocode?

Integer a, b, c
Set a = 45, b = 38
while (b != 0)
    c = (a & b) << 1
    a = a ^ b
    b = c
End while
Print a

Note: & is bitwise AND, ^ is bitwise XOR, << is left shift.

  1. 155
  2. 83
  3. 127
  4. 84

Answer: B. 83

Explanation

XOR adds without carry and (a & b) << 1 is the carry; repeating until no carry remains computes 45 + 38 = 83.

Step-by-step solution

  1. This is the add-with-bitwise-operators loop.
  2. XOR gives sum without carry, (a&b)<<1 gives the carry.
  3. Repeat until the carry is 0.
  4. Result = 45 + 38 = 83.

Why the other options are wrong

  • A. 155 is not the sum.
  • C. 127 is not the sum.
  • D. 84 is one more than the sum.

Tip: a + b = (a^b) + ((a&b)<<1).

Practise all 80 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Pseudocode & Code Tracing topics · All topics