Write an efficient program to count number of 1s in binary representation of an integer.
Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 11 Output : 3 Binary representation of 11 is 1101 and has 3 set bits
- Convert given number into it’s binary representation using bin(number) function.
- Now separate out all 1’s from binary representation of given number and print length of list of 1’s.
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python | List comprehension vs * operator
- Python List Comprehension and Slicing
- Python List Comprehension to find pair with given sum from two arrays
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Move all zeroes to end of array using List Comprehension in Python
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python List Comprehension | Three way partitioning of an array around a given range
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Count set bits in a range
- Python | Count unset bits in a range
- Python Bin | Count total bits in a number
- Python map function | Count total set bits in all numbers from 1 to n
- Check if bits of a number has count of consecutive set bits in increasing order
- Python list function | count()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.