1. Home
  2. Computer Science MCQs
  3. C Programming
  4. For an unsigned integer x, what does the expression x & (x…

Added 30 Nov -0001

For an unsigned integer x, what does the expression x & (x - 1) do?

  1. Sets the lowest clear bit of x
  2. Doubles the value of x
  3. Reverses the bits of x
  4. Clears the lowest set bit of x Correct answer

Correct answer

D. Clears the lowest set bit of x

13 views

MCQSEXAM

Explanation

Subtracting one flips every bit from the lowest set bit downward, so ANDing with the original clears exactly that lowest 1 bit and leaves the higher bits intact. Repeating the operation counts set bits in Brian Kernighan's algorithm, and a single application tells you whether x is a power of two when the result is zero. The related expression x & -x isolates the lowest set bit instead of clearing it.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs