Code:What would this function return: int foo(int a) { return !(a & (a - 1)) && a; } a)Returns non zero when a is odd b)Returns non zero when a is even c)Returns non zero when a is power of 2 d)Always return zero
Code:What would this function return: int foo(int a) { return !(a & (a - 1)) && a; } a)Returns non zero when a is odd b)Returns non zero when a is even c)Returns non zero when a is power of 2 d)Always return zero
Compile it, try it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
return !(a & (a - 1)) && a;
>> Returns non zero when a is power of 2
a&a-1 will be zero only when a is 1,2,4,8,16,32 ...