Algorithms_in_C++ 1.0.0
Set of 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 <iostream>
Include dependency graph for power_of_2.cpp:

Namespaces

namespace  bit_manipulation
 for IO operations
 

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
71 {
72 test(); // run self-test implementations
73 return 0;
74}
static void test()
Self-test implementations.
Definition power_of_2.cpp:48
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
48 {
49 // n = 4 return true
50 assert(bit_manipulation::isPowerOfTwo(4) == true);
51 // n = 6 return false
52 assert(bit_manipulation::isPowerOfTwo(6) == false);
53 // n = 13 return false
54 assert(bit_manipulation::isPowerOfTwo(13) == false);
55 // n = 64 return true
56 assert(bit_manipulation::isPowerOfTwo(64) == true);
57 // n = 15 return false
58 assert(bit_manipulation::isPowerOfTwo(15) == false);
59 // n = 32 return true
60 assert(bit_manipulation::isPowerOfTwo(32) == true);
61 // n = 97 return false
62 assert(bit_manipulation::isPowerOfTwo(97) == false);
63 // n = 1024 return true
64 assert(bit_manipulation::isPowerOfTwo(1024) == true);
65 std::cout << "All test cases successfully passed!" << std::endl;
66}
T endl(T... args)
bool isPowerOfTwo(std ::int64_t n)
The main function implements check for power of 2.
Definition power_of_2.cpp:31
Here is the call graph for this function: