DEV Community

Discussion on: Power of 2, solving a google interview question. Playing with bits.

Collapse
 
pankajtanwarbanna profile image
Pankaj Tanwar

Super quick solution would be just count the no of 1 in binary form. If its 1. Return true.

In C++ just do __builtin_popcount(n) == 1

Collapse
 
akhilpokle profile image
Akhil

Yep, it one of the solutions, you're in right direction now combine the solution of the original question with this.

hint : if n&n-1 = 0, it means that the rest of the bit is guaranteed set to 0, so no need to parse them.