Python print bits: Given a number ,the task is to count the set bits of the given number in its binary representation.
Examples:
Example1:
Input:
given number =235
Output:
The total number of set bits in the given number 235 : 6
Example2:
Input:
given number =8
Output:
The total number of set bits in the given number 8 : 1
Example3:
Input:
given number =375
Output:
The total number of set bits in the given number 375 : 7
- Python Program to Print Binary Representation of a Number
- Python Program to Find Whether a Number is a Power of Two
- Python Program to Check whether kth Bit is Set or not
Program to Count Set Bits in a Number in Python
There are several ways to count set bits in the binary representation of the given number some of them are:
- By checking bit by bit from end using modulus operator
- By checking bit by bit from end using & operator
- By converting into binary using bin() and using count() function to count set bits
- By converting into binary using bin() and using sum() function to count set bits
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1: By checking bit by bit from end using modulus operator
A simple method is to take each bit into consideration in a number (set or unset) and hold an counter to track the set bits.
Approach:
- Set the variable say count to 0 to count the total number of set bits.
- We utilize the while loop.
- We’ll keep going till the number is bigger than zero (Condition of while statement)
- Using the % operator, we will determine whether the last check bit is set or not.
- If the check bit is 1, it indicates that the bit is set, and we increment the count.
- Divide the given number by 2.
- Print the count.
Below is the implementation:
def countSetBit(numb): # checking if the given number is greater than 1 if numb > 1: # Set the variable say setbitcount to 0 to count the total number of set bits. setbitcount = 0 # looping till number greater than 0 using while loop while(numb > 0): # We will get the last check bit whether it is set bit or not using % operator checkbit = numb % 2 # checking if the check bit is 1 or not # if the check bit is 1 then increment the setbitcount if(checkbit == 1): setbitcount = setbitcount+1 # divide the number by 2 numb = numb//2 # return the setbitcount return setbitcount # Driver code given_numb = 235 # passing given number to countSetBit function to # count the total number of set bits in the given number print("The total number of set bits in the given number ", given_numb, " : ") print(countSetBit(given_numb))
Output:
The total number of set bits in the given number 235 : 6
Method #2: By checking bit by bit from end using & operator
A simple method is to take each bit into consideration in a number (set or unset) and hold an counter to track the set bits.
Approach:
- Set the variable say count to 0 to count the total number of set bits.
- We utilize the while loop.
- We’ll keep going till the number is bigger than zero (Condition of while statement)
- Using the & operator, we will determine whether the last check bit is set or not.
- If the check bit is 1, it indicates that the bit is set, and we increment the count.
- Divide the given number by 2.
- Print the count.
We use n&1 to check whether it is set bit or not.
Below is the implementation:
def countSetBit(numb): # checking if the given number is greater than 1 if numb > 1: # Set the variable say setbitcount to 0 to count the total number of set bits. setbitcount = 0 # looping till number greater than 0 using while loop while(numb > 0): # We will get the last check bit whether it is set bit or not using & operator # checking if the check bit is 1 or not # if the check bit is 1 then increment the setbitcount if(numb & 1): setbitcount = setbitcount+1 # divide the number by 2 numb = numb//2 # return the setbitcount return setbitcount # Driver code given_numb = 235 # passing given number to countSetBit function to # count the total number of set bits in the given number print("The total number of set bits in the given number ", given_numb, " : ") print(countSetBit(given_numb))
Output:
The total number of set bits in the given number 235 : 6
Method #3: By converting into binary using bin() and using count() function to count set bits
Approach:
- First we convert the given number into binary using bin() function.
- Then we count total number of ‘1’s in the binary string using count() function
- Print the count
Below is the implementation:
def countSetBit(numb): # converting given number to binary reepresentatioon using bin() binNum = bin(numb) # we count total number of '1's in the binary string using count() function setbitcount = binNum.count('1') # return the setbitcount return setbitcount # Driver code given_numb = 235 # passing given number to countSetBit function to # count the total number of set bits in the given number print("The total number of set bits in the given number ", given_numb, " : ") print(countSetBit(given_numb))
Output:
The total number of set bits in the given number 235 : 6
Method #4: By converting into binary using bin() and using sum() function to count set bits
Approach:
- First we convert the given number into binary using bin() function.
- We slice from 2 to end of the binary string using slicing (to remove 0b characters from binary string)
- Convert every character of binary string to integer using map and then convert it to list
- Then we count total number of ‘1’s in the binary string list using sum() function(because the binary string contains only 0 and 1)
- Print the count
Below is the implementation:
def countSetBit(numb): # converting given number to binary reepresentatioon using bin() binNum = bin(numb) # We will slice from 2 index to last index of the result returned by binary string binNum = binNum[2:] # Convert every character of binary string to integer using map # and then convert it to list binList = list(map(int, binNum)) # we count total number of '1's in the binary string using sum() function setbitcount = sum(binList) # return the setbitcount return setbitcount # Driver code given_numb = 235 # passing given number to countSetBit function to # count the total number of set bits in the given number print("The total number of set bits in the given number ", given_numb, " : ") print(countSetBit(given_numb))
Output:
The total number of set bits in the given number 235 : 6
Related Programs: