-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: add power of two algorithm #2224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b4161e5
feat: add power_of_two.cpp
pg2511 ca5f1d0
Merge branch 'master' into prafful-gupta
Panquesito7 ffce8b0
chore: minor changes
Panquesito7 6162506
fix: Windows CI issues
Panquesito7 19b8b22
Merge branch 'master' into prafful-gupta
Panquesito7 df16aa4
chore: remove unnecessary namespace
Panquesito7 230ef79
Merge branch 'master' into prafful-gupta
Panquesito7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @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 <cassert> /// for assert | ||
#include <iostream> /// 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 2 namespaces are required ? Is it like you would add more algorithms in this file ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only two namespaces are required in case there are a lot of functions. But in this case, it's only one function, so having one namespace should suffice.