Skip to content

Counting 0s and 1s:Update binary_count_trailing_zeros.py #12033

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 bit_manipulation/binary_count_trailing_zeros.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ def binary_count_trailing_zeros(a: int) -> int:
import doctest

doctest.testmod()


# counting number of 0s and 1s in a binary number
def count_zeros_and_ones(binary_number):
# Convert the binary number to a string if it's not already
binary_str = str(binary_number)

count_zeros = binary_str.count("0")
count_ones = binary_str.count("1")

return count_zeros, count_ones


# Example usage
binary_number = 1011001
zeros, ones = count_zeros_and_ones(binary_number)
print(f"Number of 0s: {zeros}, Number of 1s: {ones}")