Bit manipulation

Asked atCienaInfosys
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Clarify integer width, signedness, and allowed operations.
  2. 02Express the required property as bits or masks.
  3. 03Use XOR for pair cancellation when applicable.
  4. 04Use shifts and masks with explicit bounds.
  5. 05Test zero, negatives, and high-bit cases.

Reference answer

Then expect these follow-ups

  • How do you isolate the lowest set bit?

    Tests: follow-up reasoning

  • Why does x AND (x minus one) clear one bit?

    Tests: correctness reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question