Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitManipulation #773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)))