From 2c8f3025e3ecff1ca36ee615f57c52f2b869d486 Mon Sep 17 00:00:00 2001 From: rayavarapuvikram1 Date: Wed, 5 Oct 2022 20:30:18 +0530 Subject: [PATCH] Create next_highest_pow_2.py --- algorithms/bit_manipulation/next_highest_pow_2.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 algorithms/bit_manipulation/next_highest_pow_2.py diff --git a/algorithms/bit_manipulation/next_highest_pow_2.py b/algorithms/bit_manipulation/next_highest_pow_2.py new file mode 100644 index 00000000..42b1962b --- /dev/null +++ b/algorithms/bit_manipulation/next_highest_pow_2.py @@ -0,0 +1,10 @@ +''' +Finding the next highest power of 2 to the given positve integer +''' + + +def findNextPowerOf2(n): + n = n - 1 + while n & n - 1: + n = n & n - 1 + return n << 1