Skip to content

Commit

Permalink
Create power_of_two.cpp (TheAlgorithms#1315)
Browse files Browse the repository at this point in the history
* Create power_of_two.cpp

This Pull Request is for HacktoberFest 2020

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for 3d017c4

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/power_of_two.cpp

Co-authored-by: David Leal <[email protected]>

* clang-format and clang-tidy fixes for f76c100

Co-authored-by: David Leal <[email protected]>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 12, 2021
1 parent 521aa3f commit 9438ea1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
* [Ncr Modulo P](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/ncr_modulo_p.cpp)
* [Number Of Positive Divisors](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/number_of_positive_divisors.cpp)
* [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp)
* [Power Of Two](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_of_two.cpp)
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp)
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp)
* [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp)
Expand Down
66 changes: 66 additions & 0 deletions math/power_of_two.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @file
* @brief Implementation to check whether a number is a power of 2 or not.
*
* @details
* 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](https://github.com/neha-hasija17)
*/

#include <iostream> /// for std::cout

/**
* @namespace math
* @brief Mathematical algorithms
*/
namespace math {
/**
* @brief Function to test above algorithm
* @param n description
* @returns void
*/
void power_of_two(int n) {
/**
* This function finds whether a number is power of 2 or not
* @param n value for which we want to check
* prints the result, as "Yes, the number n is a power of 2" or
* "No, the number is not a power of 2" without quotes
*/
/// result stores the
/// bitwise and of n and n-1
int result = n & (n - 1);
if (result == 0) {
std::cout << "Yes, the number " << n << " is a power of 2";
} else {
std::cout << "No, the number " << n << " is not a power of 2";
}
}
} // namespace math

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
int n = 0;
/// n stores the input from the user
std::cout << "enter a number " << std::endl;
std::cin >> n;
/// function call with @param n
math::power_of_two(n);
return 0;
}

0 comments on commit 9438ea1

Please sign in to comment.