Skip to content

Instantly share code, notes, and snippets.

@aaBoustani
Last active November 30, 2017 11:21
Show Gist options
  • Select an option

  • Save aaBoustani/82a9350c6ad25fa3108f15540ab0c2ee to your computer and use it in GitHub Desktop.

Select an option

Save aaBoustani/82a9350c6ad25fa3108f15540ab0c2ee to your computer and use it in GitHub Desktop.
#include <bitset>
using namespace std;
bool is_power_of_two (int N) {
  bitset<100000> foo(N); //bitset<numberOfBitsDesired>
  return foo.count() == 1;
}
bool is_power_of_two (int N) {
return N && (!(N & (N - 1)));
}
bool is_power_of_two (int N) {
if (!N) return 0; // Case of N == 0
while (N != 1) {
if (N % 2) return 0;
N /= 2;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment