|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Find whether a given number is power of 2] |
| 4 | + * (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/) implementation |
| 5 | + * |
| 6 | + * @details |
| 7 | + * We are given a positive integer number. We need to check whether the number is power of |
| 8 | + * 2 or not. |
| 9 | + * |
| 10 | + * A binary number consists of two digits. They are 0 & 1. Digit 1 is known as |
| 11 | + * set bit in computer terms. |
| 12 | + * Worst Case Time Complexity: O(1) |
| 13 | + * Space complexity: O(1) |
| 14 | + * @author [Prafful Gupta](https://github.com/EcstaticPG-25811) |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cassert> /// for assert |
| 18 | +#include <iostream> /// for IO operations |
| 19 | + |
| 20 | +/** |
| 21 | + * @namespace bit_manipulation |
| 22 | + * @brief Bit manipulation algorithms |
| 23 | + */ |
| 24 | +namespace bit_manipulation { |
| 25 | +/** |
| 26 | + * @brief The main function implements check for power of 2 |
| 27 | + * @param n is the number who will be checked |
| 28 | + * @returns either true or false |
| 29 | + */ |
| 30 | +bool isPowerOfTwo( |
| 31 | + std ::int64_t n) { // int64_t is preferred over int so that |
| 32 | + // no Overflow can be there. |
| 33 | + |
| 34 | + return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1 |
| 35 | + // then all unset bits after the only set bit become set; and the set bit becomes unset. |
| 36 | + |
| 37 | + // If a number n is a power of 2 then bitwise and of n-1 and n will be zero. |
| 38 | + // The expression n&(n-1) will not work when n is 0. |
| 39 | + // To handle this case also, our expression will become n& (!n&(n-1)) |
| 40 | +} |
| 41 | +} // namespace bit_manipulation |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Self-test implementations |
| 45 | + * @returns void |
| 46 | + */ |
| 47 | +static void test() { |
| 48 | + // n = 4 return true |
| 49 | + assert(bit_manipulation::isPowerOfTwo(4) == true); |
| 50 | + // n = 6 return false |
| 51 | + assert(bit_manipulation::isPowerOfTwo(6) == false); |
| 52 | + // n = 13 return false |
| 53 | + assert(bit_manipulation::isPowerOfTwo(13) == false); |
| 54 | + // n = 64 return true |
| 55 | + assert(bit_manipulation::isPowerOfTwo(64) == true); |
| 56 | + // n = 15 return false |
| 57 | + assert(bit_manipulation::isPowerOfTwo(15) == false); |
| 58 | + // n = 32 return true |
| 59 | + assert(bit_manipulation::isPowerOfTwo(32) == true); |
| 60 | + // n = 97 return false |
| 61 | + assert(bit_manipulation::isPowerOfTwo(97) == false); |
| 62 | + // n = 1024 return true |
| 63 | + assert(bit_manipulation::isPowerOfTwo(1024) == true); |
| 64 | + std::cout << "All test cases successfully passed!" << std::endl; |
| 65 | +} |
| 66 | +/** |
| 67 | + * @brief Main function |
| 68 | + * @returns 0 on exit |
| 69 | + */ |
| 70 | +int main() { |
| 71 | + test(); // run self-test implementations |
| 72 | + return 0; |
| 73 | +} |
0 commit comments