PHP Program to Count set Bits in an Integer

In PHP, counting set bits (1s) in an integer's binary representation is a common programming task. A set bit refers to a binary digit that has the value 1, while a clear bit has the value 0. For example, the number 12 in binary is 1100, which has 2 set bits.

What is Binary Code?

Binary code is a system of representing information using a base-2 numeral system. It uses only two digits, 0 and 1, to represent all values. Each digit in binary code is called a bit (short for binary digit).

In binary code, each digit represents a power of 2. Starting from the rightmost digit, the powers of 2 increase from right to left. For example, in an 8-bit binary code, the rightmost bit represents 2^0 (1), the next bit represents 2^1 (2), the next represents 2^2 (4), and so on.

Example

Let's convert the decimal number 42 to binary. We divide it successively by 2 and track the remainders ?

42 ÷ 2 = 21, remainder 0

21 ÷ 2 = 10, remainder 1

10 ÷ 2 = 5, remainder 0

5 ÷ 2 = 2, remainder 1

2 ÷ 2 = 1, remainder 0

1 ÷ 2 = 0, remainder 1

Reading remainders from bottom to top gives us 101010. This binary representation has 3 set bits (1s) and 3 clear bits (0s).

Method 1: Iterative Approach

This method loops through all bits using bitwise operations ?

<?php
// Function to count set bits in an integer
function countSetBits($n)
{
    $count = 0;
    while ($n) {
        $count += $n & 1;  // Check if last bit is 1
        $n >>= 1;          // Right shift by 1
    }
    return $count;
}

// Test with number 12 (binary: 1100)
$number = 12;
echo "Number of set bits in $number: " . countSetBits($number);
?>
Number of set bits in 12: 2

Method 2: Recursive Approach

This method uses recursion to count set bits by checking each bit recursively ?

<?php
// Recursive function to count set bits
function countSetBitsRecursive($n)
{
    // Base case
    if ($n == 0)
        return 0;
    
    // Check last bit and recurse with right-shifted number
    return ($n & 1) + countSetBitsRecursive($n >> 1);
}

// Test with number 123 (binary: 1111011)
$n = 123;
echo "Number of set bits in $n: " . countSetBitsRecursive($n);
?>
Number of set bits in 123: 6

Method 3: Using Built-in Function

PHP provides the substr_count() function which can count occurrences of '1' in a binary string ?

<?php
// Using built-in function
function countSetBitsBuiltin($n)
{
    return substr_count(decbin($n), '1');
}

$number = 42;
echo "Set bits in $number using built-in function: " . countSetBitsBuiltin($number);
?>
Set bits in 42 using built-in function: 3

Comparison

Method Time Complexity Space Complexity Approach
Iterative O(log n) O(1) Bitwise operations
Recursive O(log n) O(log n) Function calls
Built-in O(log n) O(log n) String conversion

Conclusion

All three methods effectively count set bits in an integer. The iterative approach is most memory-efficient, while the built-in function offers the simplest implementation. Choose based on your performance requirements and code readability preferences.

Updated on: 2026-03-15T10:37:01+05:30

454 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements