1.

Which of these expressions will return true if the input integer v is a power of two?(a) (v | (v + 1)) == 0;(b) (~v & (v – 1)) == 0;(c) (v | (v – 1)) == 0;(d) (v & (v – 1)) == 0;I got this question in an international level competition.The query is from Integer Types topic in portion Types, Pointers, Arrays & Structures in C++ of C++

Answer»

Right answer is (d) (v & (v – 1)) == 0;

Explanation: POWER of TWO integers have a SINGLE set bit followed by unset bits.



Discussion

No Comment Found

Related InterviewSolutions