ProgrammingHardHCL · Tech Mahindra pattern

Assume 32-bit int. What does this snippet do, and what is printed for x=10?

Assume 32-bit int. What does this snippet do, and what is printed for x=10?
int x = 10;
int y = x & (x - 1);
printf("%d", y);
  1. A. 10
  2. B. 8
  3. C. 2
  4. D. 0

Correct answer: B. 8

x & (x-1) is the classic trick to clear the lowest set bit of x. 10 is 1010; 9 is 1001; ANDing them gives 1000 = 8, which is 10 with its lowest set bit (the '2' bit) turned off.

More C Programming questions

Exams that ask this topic