Skip to content

Commit ca56b8e

Browse files
authored
Update Check_whether_a_number_is_divisible_by_2^k_or_not.cpp
1 parent 6d1bb39 commit ca56b8e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

bit_manipulation/Check_whether_a_number_is_divisible_by_2^k_or_not.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
* @author [Tirth Patel](https://github.com/Tirth9978)
1818
*/
1919

20+
21+
/*
22+
23+
Explanation :
24+
--> LL << k shifts 1 left by k positions, giving us 2 ^ k
25+
--> Subtracting 1 from it gives a binary number with the last k bits set.
26+
--> Bitwise AND with n checks if the last k bits of n are zero.
27+
28+
*/
29+
30+
31+
2032
#include <cassert> /// for assert
2133
#include <cstdint> /// for int64_t
2234
#include <iostream> /// for IO operations
@@ -68,7 +80,7 @@ static void test()
6880
assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 60) - 1, 59) == false); // Not divisible by 2^59
6981
assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 40, 35) == true); // 2^40 % 2^35 = 0
7082
assert(bit_manipulation::isDivisibleBy2PowerK(1LL << 62, 62) == true); // 2^62 % 2^62 = 0
71-
// assert(bit_manipulation::isDivisibleBy2PowerK((1LL << 63) - 1, 63) == false); // Largest 64-bit integer - 1, not divisible by 2^63
83+
7284

7385
// Edge Cases for Small Numbers
7486
assert(bit_manipulation::isDivisibleBy2PowerK(0, 5) == true); // 0 is divisible by any power of 2

0 commit comments

Comments
 (0)