Last active
November 30, 2017 11:21
-
-
Save aaBoustani/82a9350c6ad25fa3108f15540ab0c2ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bitset> | |
| using namespace std; | |
| bool is_power_of_two (int N) { | |
| bitset<100000> foo(N); //bitset<numberOfBitsDesired> | |
| return foo.count() == 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bool is_power_of_two (int N) { | |
| return N && (!(N & (N - 1))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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