You have to check bit by bit, with a function more or less like this:
function p2(n) {
if (n === 0) return false;
while (n) {
if (n & 1 && n !== 1) return false;
n >>= 1;
}
return true;
}
Some CPU instruction sets have included a "count set bits" operation (the ancient CDC Cyber series was one). It's useful for some data structures implemented as bit collections. If you've got a set implemented as a string of integers, with bit positions corresponding to elements of the set data type, then getting the cardinality involves counting bits.
edit wow looking into Ted Hopp's answer I stumbled across this:
function p2(n) {
return n !== 0 && (n & (n - 1)) === 0;
}
That's from this awesome collection of "tricks". Things like this problem are good reasons to study number theory :-)