1.

Which of these expressions will make the rightmost set bit zero in an input integer x?(a) x = x | (x-1)(b) x = x & (x-1)(c) x = x | (x+1)(d) x = x & (x+2)I got this question in an interview for job.Question is from Integer Types in division Types, Pointers, Arrays & Structures in C++ of C++

Answer»

The correct OPTION is (b) x = x & (x-1)

The explanation: If x is odd the last bit will be 1 and last bit of x-1 will BECOME 0. If x is EVEN then last bit of x will be 0 and last bit of x-1 will become 1. In both case AND operation of 1 and 0 will be 0. Hence last bit of final x will be 0.



Discussion

No Comment Found

Related InterviewSolutions