# Algorithms_in_C++: math/power_of_two.cpp File Reference

Loading...
Searching...
No Matches
power_of_two.cpp File Reference

Implementation to check whether a number is a power of 2 or not. More...

`#include <iostream>`
`#include <cassert>`
Include dependency graph for power_of_two.cpp:

## Namespaces

namespace | math
| for std::rand

## Functions

int | math::power_of_two (int n)
| This function finds whether a number is power of 2 or not. More...
static void | test ()
| Self-test implementations. More...
void | user_input_test ()
| Take user input in the test cases (optional; currently commented) More...
int | main ()
| Main function. More...

## Detailed Description

Implementation to check whether a number is a power of 2 or not.

This algorithm uses bit manipulation to check if a number is a power of 2 or not.

### Algorithm

Let the input number be n, then the bitwise and between n and n-1 will let us know whether the number is power of 2 or not

For Example, If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then the result will be zero, which indicates that it is the power of 2 If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then the result will not be zero , which indicates that it is not the power of 2

Note
  This implementation is better than naive recursive or iterative approach.

Author
  Neha Hasija
  Rijul.S

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
// uncomment the line below to take user inputs
//user_input_test();
return 0;
}
test
static void test()
Self-test implementations.
Definition: power_of_two.cpp:59
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
std::cout << "First case testing... \n"; // for n = 32 return 1
assert(math::power_of_two(32) == 1);
std::cout << "\nPassed!\n";
std::cout << "Second case testing... \n"; // for n = 5 return 0
assert(math::power_of_two(5) == 0);
std::cout << "\nPassed!\n";
std::cout << "Third case testing... \n"; // for n = 232 return 0
assert(math::power_of_two(232) == 0);
std::cout << "\nPassed!\n";
std::cout << "\nAll test cases have successfully passed!\n";
}
std::cout
math::power_of_two
int power_of_two(int n)
This function finds whether a number is power of 2 or not.
Definition: power_of_two.cpp:42
Here is the call graph for this function:

## user_input_test()

void user_input_test | ( |  | ) |

Take user input in the test cases (optional; currently commented)

Returns
  void

function call with

Parameters

  n |

{
int n = 0; // input from user
std::cout << "Enter a number " << std::endl;
std::cin >> n;
/// function call with @param n
int result = math::power_of_two(n);
if (result == 1) {
std::cout << "Yes, the number " << n << " is a power of 2\n";
}
else {
std::cout << "No, the number " << n << " is not a power of 2\n";
}
}
std::cin
std::endl
T endl(T... args)
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
Here is the call graph for this function: