Threads
power_of_2 ( 19228 ) - power_of_2 ( 19228 ) stack: main(bit_manipulation/power_of_2.cpp:71)
/**
* @file
* @brief [Find whether a given number is power of 2]
* (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/)
* implementation
*
* @details
* We are given a positive integer number. We need to check whether the number
* is power of 2 or not.
*
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
* set bit in computer terms.
* Worst Case Time Complexity: O(1)
* Space complexity: O(1)
* @author [Prafful Gupta](https://github.com/EcstaticPG-25811)
*/
#include /// for assert
#include /// for IO operations
/**
* @namespace bit_manipulation
* @brief Bit manipulation algorithms
*/
namespace bit_manipulation {
/**
* @brief The main function implements check for power of 2
* @param n is the number who will be checked
* @returns either true or false
*/
bool isPowerOfTwo(std ::int64_t n) { // int64_t is preferred over int so that
// no Overflow can be there.
return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1
// then all unset bits after the only set bit become set; and the set bit
// becomes unset.
// If a number n is a power of 2 then bitwise and of n-1 and n will be zero.
// The expression n&(n-1) will not work when n is 0.
// To handle this case also, our expression will become n& (!n&(n-1))
}
} // namespace bit_manipulation
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// n = 4 return true
assert(bit_manipulation::isPowerOfTwo(4) == true);
// n = 6 return false
assert(bit_manipulation::isPowerOfTwo(6) == false);
// n = 13 return false
assert(bit_manipulation::isPowerOfTwo(13) == false);
// n = 64 return true
assert(bit_manipulation::isPowerOfTwo(64) == true);
// n = 15 return false
assert(bit_manipulation::isPowerOfTwo(15) == false);
// n = 32 return true
assert(bit_manipulation::isPowerOfTwo(32) == true);
// n = 97 return false
assert(bit_manipulation::isPowerOfTwo(97) == false);
// n = 1024 return true
assert(bit_manipulation::isPowerOfTwo(1024) == true);
std::cout << "All test cases successfully passed!" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}
Variables All
No. | From | Name | Value |
---|---|---|---|
NA | NA | NA | NA |
×
Commands and Shortcuts
No. | Command | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | algorithms-cpp |
Update: | 20240212 |
Commit: | 2dadbf73f |
Source Code: | bit_manipulation.power_of_2 |
BuildTool: | cpp17 |
Compiler: | cpp17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |