From 6d1bb39a56df19f94be91f29902f5b684c4182b1 Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Sun, 30 Mar 2025 11:04:55 +0530 Subject: [PATCH 1/2] Add check for divisibility by 2^k with test cases --- ...er_a_number_is_divisible_by_2^k_or_not.cpp | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp diff --git a/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp b/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp new file mode 100644 index 00000000000..330c6a5f0b3 --- /dev/null +++ b/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp @@ -0,0 +1,91 @@ +/** + * Check whether a number is divisible by 2^k or not + * + * + * We are given a positive integer `n` and an integer `k`. + * We need to check whether `n` is divisible by `2^k`. + * + * - Bitwise AND operation is used to efficiently check + * if the last k bits of n are zero. + * - If (n & (2^k - 1)) equals 0, then the number is divisible by 2^k. + * + * + * + * Worst Case Time Complexity: O(1) + * Space Complexity: O(1) + * + * @author [Tirth Patel](https://github.com/Tirth9978) + */ + +#include /// for assert +#include /// for int64_t +#include /// for IO operations + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation +{ + + /** + * @brief Check if a number `n` is divisible by 2^k + * @param n The input number + * @param k The power of 2 + * @returns `true` if divisible, otherwise `false` + */ + bool isDivisibleBy2PowerK(std::int64_t n, int k) + { + // Check if the last k bits of n are zero + return (n & ((1LL << k) - 1)) == 0; + } +} + +static void test() +{ + // Test cases to check divisibility by 2^k + + // 8 is divisible by 2^3 + assert(bit_manipulation::isDivisibleBy2PowerK(8, 3) == true); + // 10 is not divisible by 2^2 + assert(bit_manipulation::isDivisibleBy2PowerK(10, 2) == false); + // 64 is divisible by 2^6 + assert(bit_manipulation::isDivisibleBy2PowerK(64, 6) == true); + // 15 is not divisible by 2^3 + assert(bit_manipulation::isDivisibleBy2PowerK(15, 3) == false); + // 32 is divisible by 2^5 + assert(bit_manipulation::isDivisibleBy2PowerK(32, 5) == true); + // 100 is not divisible by 2^5 + assert(bit_manipulation::isDivisibleBy2PowerK(100, 5) == false); + // 128 is divisible by 2^7 + assert(bit_manipulation::isDivisibleBy2PowerK(128, 7) == true); + // 1024 is divisible by 2^10 + assert(bit_manipulation::isDivisibleBy2PowerK(1024, 10) == true); + + // Edge Cases for Large Numbers + assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 20, 20) == true); // 2^20 % 2^20 = 0 + assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 50), 50) == true); // 2^50 % 2^50 = 0 + assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 60) - 1, 59) == false); // Not divisible by 2^59 + assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 40, 35) == true); // 2^40 % 2^35 = 0 + assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 62, 62) == true); // 2^62 % 2^62 = 0 + // assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 63) - 1, 63) == false); // Largest 64-bit integer - 1, not divisible by 2^63 + + // Edge Cases for Small Numbers + assert(bit_manipulation::isDivisibleBy2PowerK(0, 5) == true); // 0 is divisible by any power of 2 + assert(bit_manipulation::isDivisibleBy2PowerK(1, 0) == true); // Any number is divisible by 2^0 + assert(bit_manipulation::isDivisibleBy2PowerK(2, 1) == true); // 2 % 2^1 = 0 + assert(bit_manipulation::isDivisibleBy2PowerK(3, 1) == false); // 3 % 2 != 0 + assert(bit_manipulation::isDivisibleBy2PowerK(4, 2) == true); // 4 % 4 = 0 + + 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; +} From ca56b8e0db34ca428c58c95b286791d57e421a5e Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Sun, 30 Mar 2025 11:12:12 +0530 Subject: [PATCH 2/2] Update Check_whether_a_number_is_divisible_by_2^k_or_not.cpp --- ...whether_a_number_is_divisible_by_2^k_or_not.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp b/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp index 330c6a5f0b3..bd8e191f09f 100644 --- a/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp +++ b/bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp @@ -17,6 +17,18 @@ * @author [Tirth Patel](https://github.com/Tirth9978) */ + +/* + +Explanation : +--> LL << k shifts 1 left by k positions, giving us 2 ^ k +--> Subtracting 1 from it gives a binary number with the last k bits set. +--> Bitwise AND with n checks if the last k bits of n are zero. + +*/ + + + #include /// for assert #include /// for int64_t #include /// for IO operations @@ -68,7 +80,7 @@ static void test() assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 60) - 1, 59) == false); // Not divisible by 2^59 assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 40, 35) == true); // 2^40 % 2^35 = 0 assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 62, 62) == true); // 2^62 % 2^62 = 0 - // assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 63) - 1, 63) == false); // Largest 64-bit integer - 1, not divisible by 2^63 + // Edge Cases for Small Numbers assert(bit_manipulation::isDivisibleBy2PowerK(0, 5) == true); // 0 is divisible by any power of 2