- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of bit 1 in the given number in Python

Suppose we have a number n, we have to find the number of bit 1 present in the binary representation of that number.

So, if the input is like 12, then the output will be 2

To solve this, we will follow these steps −

- count := 0
- while n is non-zero, do
- count := count + (n AND 1)
- n := floor of (n / 2)

- return count

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, n): count = 0 while (n): count += n & 1 n >>= 1 return count ob = Solution() print(ob.solve(12))

## Input

12

## Output

2

- Related Articles
- 8085 program to count number of once in the given 8-bit number
- Program to find the sum of all digits of given number in Python
- Program to find number of square submatrices with 1 in python
- Python Program to Clear the Rightmost Set Bit of a Number
- 8085 program to find minimum value of digit in the 8 bit number
- 8085 program to find square of a 8 bit number
- How to find the number of digits in a given number using Python?
- 8086 program to find sum of digits of 8 bit number
- 8085 program to find sum of digits of 8 bit number
- Program to find number of given operations required to reach Target in Python
- Program to find out the number of special numbers in a given range in Python
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Program to find number of sublists whose sum is given target in python
- Program to find out number of distinct substrings in a given string in python

Advertisements