Skip to main content

Count set bits in an integer

The total set bits in an integer refer to the total number of binary digits (bits) that have a value of 1 in the binary representation of the integer.

Counting total set bits in a number

For example, the decimal number 342 has a binary representation of 101010110, which means it has five set bits.

For other example, the decimal number 7 has a binary representation of 111, which means it has three set bits. Similarly, the decimal number 15 has a binary representation of 1111, which means it has four set bits.

Counting the total number of set bits in an integer is a common task in computer programming, especially when working with bitwise operations. There are various algorithms and techniques for efficiently counting set bits in an integer, depending on the programming language and platform being used.

Other examples

Input : 10 (decimal)
Output : 2  (1010)
             - -  (2 set bits)

Input : 15 (decimal)
Output : 4  (1111)
             ---- (4 set bits) 

Here given code implementation process.

With Loop

Without Loop


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment