Bit Manipulation Questions with Answers

45 practice questions on Bit Manipulation (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 45 free in the AptiNest app.

Sample questions

Question 1Easy

n = 82 (binary 1010010). What is n | (1 << 3)?

  1. 83
  2. 90
  3. 656
  4. 85

Answer: B. 90

Explanation

1 << 3 = 1000; OR sets bit 3: 1011010 = 90.

Step-by-step solution

  1. 82 = 1010010.
  2. 1 << 3 = 0001000.
  3. OR sets bit 3: 1011010 = 90.

Why the other options are wrong

  • A. 83 sets bit 0 (adds 1), not bit 3.
  • C. 656 is 82 << 3, a shift, not setting a bit.
  • D. 85 does not come from setting bit 3.

Tip: Set bit k: n | (1 << k).

Question 2Easy

n = 38 (binary 100110). What is n ^ (1 << 4)?

  1. 54
  2. 42
  3. 22
  4. 38

Answer: A. 54

Explanation

1 << 4 = 10000; XOR toggles bit 4 from 0 to 1: 110110 = 54.

Step-by-step solution

  1. 38 = 100110; bit 4 (value 16) is 0.
  2. XOR with 10000 flips it to 1.
  3. 110110 = 54.

Why the other options are wrong

  • B. 42 does not come from toggling bit 4.
  • C. 22 subtracts 16, as if bit 4 were already set; it was 0.
  • D. XOR with a nonzero mask always changes the value.

Tip: Toggle bit k: n ^ (1 << k).

Question 3Easy

What is the Gray code of 13, computed as n ^ (n >> 1)?

  1. 12 (binary 1100)
  2. 13 (binary 1101)
  3. 11 (binary 1011)
  4. 6 (binary 110)

Answer: C. 11 (binary 1011)

Explanation

13 = 1101 and 13 >> 1 = 0110;
XOR gives 1011 = 11.
Consecutive Gray codes differ in exactly one bit.

Step-by-step solution

  1. 13 = 1101.
  2. 13 >> 1 = 0110.
  3. 1101 ^ 0110 = 1011 = 11.

Why the other options are wrong

  • A. 12 does not come from n ^ (n >> 1).
  • B. 13 is the number itself; the Gray code changes it.
  • D. 6 is only 13 >> 1, before the XOR.

Tip: Gray = n ^ (n >> 1).

Question 4Easy

Every element of [7, 3, 5, 4, 5, 3, 4] appears twice except one. What does XOR-ing all the elements give?

  1. 0
  2. 7
  3. 1
  4. 8

Answer: B. 7

Explanation

x ^ x = 0 and x ^ 0 = x, so the pairs cancel and 7 remains.

Step-by-step solution

  1. Pairs: 3, 3; 5, 5; 4, 4.
  2. Each pair XORs to 0.
  3. Only 7 remains.

Why the other options are wrong

  • A. 0 would mean no unpaired element.
  • C. 1 does not result; pairs cancel exactly.
  • D. 8 is not even in the array.

Tip: XOR all elements to find the one that appears an odd number of times.

Question 5Medium

What is 1 ^ 2 ^ 3 ^ ... ^ 15 (^ is XOR)?

  1. 1
  2. 16
  3. 15
  4. 0

Answer: D. 0

Explanation

XOR of 1..n follows the cycle by n mod 4: n, 1, n + 1, 0. Since 15 mod 4 = 3, the result is 0.

Step-by-step solution

  1. Use the n mod 4 pattern: n, 1, n + 1, 0.
  2. 15 mod 4 = 3.
  3. Result = 0.

Why the other options are wrong

  • A. 1 is the case n mod 4 = 1.
  • B. 16 is the case n mod 4 = 2 (n + 1).
  • C. 15 is the case n mod 4 = 0.

Tip: XOR 1..n is 0 whenever n mod 4 = 3.

Question 6Medium

What is 1 ^ 2 ^ 3 ^ ... ^ 10 (^ is XOR)?

  1. 11
  2. 0
  3. 1
  4. 10

Answer: A. 11

Explanation

XOR of 1..n follows the cycle by n mod 4: n, 1, n + 1, 0. Since 10 mod 4 = 2, the result is 11.

Step-by-step solution

  1. XOR 1..n depends on n mod 4: 0 -> n, 1 -> 1, 2 -> n + 1, 3 -> 0.
  2. 10 mod 4 = 2.
  3. Result = 10 + 1 = 11.

Why the other options are wrong

  • B. 0 is the case n mod 4 = 3.
  • C. 1 is the case n mod 4 = 1.
  • D. 10 is the case n mod 4 = 0.

Tip: n mod 4 = 0, 1, 2, 3 gives n, 1, n + 1, 0.

Question 7Medium

What is the smallest power of two that is >= 37?

  1. 38
  2. 64
  3. 128
  4. 32

Answer: B. 64

Explanation

36 = 100100; smearing its bits right and adding 1 gives 64.

Step-by-step solution

  1. Powers of two: 32, 64, 128.
  2. 32 < 37, so too small.
  3. 64 is the first >= 37.

Why the other options are wrong

  • A. 38 is not a power of two.
  • C. 128 works but is not the smallest.
  • D. 32 is less than 37.

Tip: Next power of two >= n: 1 << (n - 1).bit_length().

Question 8Medium

What is the total number of 1 bits in the binary representations of all integers from 0 to 7?

  1. 12
  2. 14
  3. 11
  4. 13

Answer: A. 12

Explanation

Using dp[i] = dp[i >> 1] + (i & 1) and summing gives 12.

Step-by-step solution

  1. Bits of 0..7: 0, 1, 1, 2, 1, 2, 2, 3.
  2. Sum = 12.
  3. Check: 3 bits x 8 numbers / 2 = 12, since each bit is 1 half the time.

Why the other options are wrong

  • B. 14 overcounts; each of 3 bit positions is 1 in exactly 4 of the 8 numbers.
  • D. 13 overcounts by one.

Tip: Total set bits in 0..2^k - 1 = k x 2^(k-1).

Question 9Hard

What does this code print?

def f(n):
    return n > 0 and (n & (n - 1)) == 0

print([k for k in range(1, 40) if f(k)])
  1. [1, 2, 4, 6, 8, 16, 32]
  2. [2, 4, 8, 16, 32]
  3. [1, 2, 4, 8, 16, 32]
  4. [1, 2, 4, 8, 16]

Answer: C. [1, 2, 4, 8, 16, 32]

Explanation

f tests for a single set bit, i.e. powers of two below 40.

Step-by-step solution

  1. n & (n - 1) == 0 means n has exactly one set bit.
  2. n > 0 excludes 0.
  3. Powers of two in 1..39: 1, 2, 4, 8, 16, 32.

Why the other options are wrong

  • A. 6 = 110 has two set bits.
  • B. 1 = 2^0 is also a power of two.
  • D. 32 is below 40 and must be included.

Tip: Remember 1 is 2^0.

Question 10Hard

What does this code print?

res = 0
for x in [3, 5, 6]:
    res = (res << 2) | (x & 3)
print(res)
  1. 54
  2. 62
  3. 45
  4. 27

Answer: A. 54

Explanation

Each step shifts left by 2 and appends the low 2 bits:
3 & 3 = 3,
5 & 3 = 1,
6 & 3 = 2, giving 11 01 10 = 54.

Step-by-step solution

  1. x & 3 keeps the low 2 bits: 3 -> 11, 5 -> 01, 6 -> 10.
  2. Shift left 2 and append each.
  3. Result 110110 = 54.

Why the other options are wrong

  • B. 62 does not match the packed bits 11 01 10.
  • C. 45 packs the values in a different order.
  • D. 27 is 11011, dropping a bit.

Tip: Pack: res = (res << k) | (v & ((1 << k) - 1)).

Practise all 45 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 Data Structures & Algorithms topics · All topics