From b4161e5be8444bfbb7a24dbfeffaf4cac00ac789 Mon Sep 17 00:00:00 2001 From: Prafful Gupta Date: Sat, 15 Oct 2022 02:28:43 +0530 Subject: [PATCH 1/4] feat: add power_of_two.cpp --- bit_manipulation/power_of_two.cpp | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 bit_manipulation/power_of_two.cpp diff --git a/bit_manipulation/power_of_two.cpp b/bit_manipulation/power_of_two.cpp new file mode 100644 index 00000000000..7ae84cce9eb --- /dev/null +++ b/bit_manipulation/power_of_two.cpp @@ -0,0 +1,75 @@ +/** + * @file + * @brief Implementation to [Find whether a given number is power of 2] + * (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/). + * + * @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 /// for assert +#include /// for IO operations +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace count_of_set_bits + * @brief Functions for the [check power of + * 2](https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/) + * implementation + */ +namespace count_of_set_bits { +/** + * @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 && not (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 count_of_set_bits +} // namespace bit_manipulation + +static void test() { + // n = 4 return true + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(4) == true); + // n = 6 return false + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(6) == false); + // n = 13 return false + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(13) == false); + // n = 64 return true + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(64) == true); + // n = 15 return false + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(15) == false); + // n = 32 return true + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(32) == true); + // n = 97 return false + assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(97) == false); + // n = 1024 return true + assert(bit_manipulation::count_of_set_bits::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; +} \ No newline at end of file From ffce8b000a00e4d6a4db9921ef05c7c54d3703f9 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 27 Dec 2022 11:54:55 -0600 Subject: [PATCH 2/4] chore: minor changes --- .../{power_of_two.cpp => power_of_2.cpp} | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) rename bit_manipulation/{power_of_two.cpp => power_of_2.cpp} (94%) diff --git a/bit_manipulation/power_of_two.cpp b/bit_manipulation/power_of_2.cpp similarity index 94% rename from bit_manipulation/power_of_two.cpp rename to bit_manipulation/power_of_2.cpp index 7ae84cce9eb..2b8c517aa29 100644 --- a/bit_manipulation/power_of_two.cpp +++ b/bit_manipulation/power_of_2.cpp @@ -1,7 +1,7 @@ /** * @file - * @brief Implementation to [Find whether a given number is power of 2] - * (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/). + * @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 @@ -13,8 +13,10 @@ * Space complexity: O(1) * @author [Prafful Gupta](https://github.com/EcstaticPG-25811) */ + #include /// for assert #include /// for IO operations + /** * @namespace bit_manipulation * @brief Bit manipulation algorithms @@ -46,6 +48,10 @@ bool isPowerOfTwo( } // namespace count_of_set_bits } // namespace bit_manipulation +/** + * @brief Self-test implementations + * @returns void + */ static void test() { // n = 4 return true assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(4) == true); @@ -72,4 +78,4 @@ static void test() { int main() { test(); // run self-test implementations return 0; -} \ No newline at end of file +} From 61625065067ff1822e475f3739f5c0fa9c88b984 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 27 Dec 2022 21:01:41 +0000 Subject: [PATCH 3/4] fix: Windows CI issues --- bit_manipulation/power_of_2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/power_of_2.cpp b/bit_manipulation/power_of_2.cpp index 2b8c517aa29..57a3dcaf279 100644 --- a/bit_manipulation/power_of_2.cpp +++ b/bit_manipulation/power_of_2.cpp @@ -38,7 +38,7 @@ bool isPowerOfTwo( std ::int64_t n) { // int64_t is preferred over int so that // no Overflow can be there. - return n > 0 && not (n & n - 1); // If we subtract a power of 2 numbers by 1 + 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. From df16aa45e3cdd6514605a1a9b7055a3726d0adf6 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 3 Feb 2023 15:11:12 -0600 Subject: [PATCH 4/4] chore: remove unnecessary namespace --- bit_manipulation/power_of_2.cpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/bit_manipulation/power_of_2.cpp b/bit_manipulation/power_of_2.cpp index 57a3dcaf279..c5c11188964 100644 --- a/bit_manipulation/power_of_2.cpp +++ b/bit_manipulation/power_of_2.cpp @@ -22,13 +22,6 @@ * @brief Bit manipulation algorithms */ namespace bit_manipulation { -/** - * @namespace count_of_set_bits - * @brief Functions for the [check power of - * 2](https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/) - * implementation - */ -namespace count_of_set_bits { /** * @brief The main function implements check for power of 2 * @param n is the number who will be checked @@ -45,7 +38,6 @@ bool isPowerOfTwo( // 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 count_of_set_bits } // namespace bit_manipulation /** @@ -54,21 +46,21 @@ bool isPowerOfTwo( */ static void test() { // n = 4 return true - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(4) == true); + assert(bit_manipulation::isPowerOfTwo(4) == true); // n = 6 return false - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(6) == false); + assert(bit_manipulation::isPowerOfTwo(6) == false); // n = 13 return false - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(13) == false); + assert(bit_manipulation::isPowerOfTwo(13) == false); // n = 64 return true - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(64) == true); + assert(bit_manipulation::isPowerOfTwo(64) == true); // n = 15 return false - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(15) == false); + assert(bit_manipulation::isPowerOfTwo(15) == false); // n = 32 return true - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(32) == true); + assert(bit_manipulation::isPowerOfTwo(32) == true); // n = 97 return false - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(97) == false); + assert(bit_manipulation::isPowerOfTwo(97) == false); // n = 1024 return true - assert(bit_manipulation::count_of_set_bits::isPowerOfTwo(1024) == true); + assert(bit_manipulation::isPowerOfTwo(1024) == true); std::cout << "All test cases successfully passed!" << std::endl; } /**