3 ms·
I'd first ask the interviewer whether they're ok with a function is_zero_or_two_power...
by tromp 7y ago
I'd first ask the interviewer whether they're ok with a function is_zero_or_two_power...
- mkl 7y agoIsn't that what the "x &&" is for? For x = 0, (x - 1) is 0xffffffff, x & (x-1) is 0, !(x & (x-1)) is 1, and x && !(x & (x-1)) is 0.
- tromp 7y agoYes, that's exactly what it's for. The answer I hope for is that they don't care about the case of x==0, so that I can leave out the "x &&" part, leaving the noticeably simpler answer (x & x-1) == 0