TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
power_of_2.cpp File Reference

[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 More...

#include <cassert>
#include <cstdint>
#include <iostream>
Include dependency graph for power_of_2.cpp:

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for assert
 

Functions

bool bit_manipulation::isPowerOfTwo (std ::int64_t n)
 The main function implements check for power of 2.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

[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

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

Definition in file power_of_2.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 72 of file power_of_2.cpp.

72 {
73 test(); // run self-test implementations
74 return 0;
75}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 49 of file power_of_2.cpp.

49 {
50 // n = 4 return true
51 assert(bit_manipulation::isPowerOfTwo(4) == true);
52 // n = 6 return false
53 assert(bit_manipulation::isPowerOfTwo(6) == false);
54 // n = 13 return false
55 assert(bit_manipulation::isPowerOfTwo(13) == false);
56 // n = 64 return true
57 assert(bit_manipulation::isPowerOfTwo(64) == true);
58 // n = 15 return false
59 assert(bit_manipulation::isPowerOfTwo(15) == false);
60 // n = 32 return true
61 assert(bit_manipulation::isPowerOfTwo(32) == true);
62 // n = 97 return false
63 assert(bit_manipulation::isPowerOfTwo(97) == false);
64 // n = 1024 return true
65 assert(bit_manipulation::isPowerOfTwo(1024) == true);
66 std::cout << "All test cases successfully passed!" << std::endl;
67}
bool isPowerOfTwo(std ::int64_t n)
The main function implements check for power of 2.