Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions BitManipulation/countSetBits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Function to get no of set bits in binary
# representation of positive integer n (iterative approach)
def countSetBits(n):
count = 0
while (n):
count += n & 1
n >>= 1
return count


# Program to test function countSetBits
# std input would also work
i = 9
print(countSetBits(i))

# contributed by
# Sampark Sharma
12 changes: 12 additions & 0 deletions BitManipulation/nextpowOf2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def nextPowOf2(n):
p = 1
if (n and not(n & (n - 1))):
return n
while (p < n) :
p <<= 1
return p;

t = int(input())
for i in range(t):
n= int(input())
print("Next Power of 2 " + str(nextPowOf2(n)))