c++ - shortest way to check if a positive integer is a power of two -
this question has answer here:
- how check if number power of 2 22 answers
this question job interview...
use template write c++ function checks if positive integer power of two.
bool p(int n) { return ********; }
you have replace 8 '*' symbols other symbols in order make function work correctly.
best approach this:
bool p(int n) { return !(n&=n-1); }
unfortunately wrong, because there 9 symbols here...
ideas?
why assign n
? remove =
, have 1 character less. create temporary instead, logic won't change.
Comments
Post a Comment