C/C++ Arena

Step 5 of 5

Challenge: power of two

A classic bit trick: a positive number is a power of two exactly when it has a single 1 bit. And x & (x - 1) clears the lowest set bit, so for powers of two it gives 0.

Your turn: write int is_pow2(unsigned x) returning 1 or 0, without loops.

Previous: Flags