PHP Program to Count Zeros and Ones in Binary Representation of a Number
Counting the number of zeros and ones in the binary representation of a number is a common task in computer science and programming. In this article, we will explore how to achieve this in PHP using different approaches.
Table of Content
- Using Built-in Functions
- Using Bitwise Operations
- Using Recursive Approach
Count Zeros and Ones in Binary Representation of a Number using Built-in Functions
This PHP program uses built-in functions to efficiently count the number of zeros and ones in the binary representation of a given number. It first converts the number to binary using decbin( ), then uses substr_count( ) to count the occurrences of “0” and “1” in the binary string. The counts are returned as an associative array.
Example: Implementation to count zeros and ones in binary representation of a number.
PHP
<?php function countZeros_Ones( $number ) { $binary = decbin ( $number ); $zeros = substr_count( $binary , "0" ); $ones = substr_count( $binary , "1" ); return [ "zeros" => $zeros , "ones" => $ones ]; } // Driver code $number = 10; $result = countZeros_Ones( $number ); echo "Binary representation of $number: " . decbin ( $number ) . "\n" ; echo "Number of zeros: " . $result [ "zeros" ] . "\n" ; echo "Number of ones: " . $result [ "ones" ] . "\n" ; ?> |
Binary representation of 10: 1010 Number of zeros: 2 Number of ones: 2
Explanation:
- The
decbin
function converts the decimal number to its binary representation. - The
substr_count( )
function counts the number of occurrences of ‘0’ and ‘1’ in the binary string. - The function returns an associative array containing the count of zeros and ones.
Count Zeros and Ones in Binary Representation of a Number using Bitwise Operations
This PHP program uses bitwise operations to count the number of zeros and ones in the binary representation of a given number. It iterates through each bit of the number, checking if it’s a zero or a one using the bitwise AND operator (‘&’). If it’s a one, it increments the $ones counter; otherwise, it increments the $zeros counter. The program then shifts the number one bit to the right (`$number >> 1`) to move to the next bit. This process continues until the number becomes zero. The counts are returned as an associative array.
Example: Implementation to count zeros and ones in binary representation of a number.
PHP
<?php function countZeros_Ones( $number ) { $zeros = 0; $ones = 0; while ( $number > 0) { if ( $number & 1) { $ones ++; } else { $zeros ++; } $number = $number >> 1; } return [ "zeros" => $zeros , "ones" => $ones ]; } // Driver code $number = 99; $result = countZeros_Ones( $number ); echo "Binary representation of $number: " . decbin ( $number ) . "\n" ; echo "Number of zeros: " . $result [ "zeros" ] . "\n" ; echo "Number of ones: " . $result [ "ones" ] . "\n" ; ?> |
Binary representation of 99: 1100011 Number of zeros: 3 Number of ones: 4
Explanation:
- The function uses a while loop to iterate through each bit of the number.
- The
&
operator is used to check if the least significant bit is 1 or 0. - The
>>
operator is used to right shift the number by one bit in each iteration. - The function keeps a count of zeros and ones and returns them in an associative array.
Count Zeros and Ones in Binary Representation of a Number using Recursive Approach
This PHP program counts the number of zeros and ones in the binary representation of a given number using a recursive approach. It recursively divides the number by 2, counting the remainder as either a zero or a one, until the number becomes zero. The counts are returned as an associative array.
Example: Implementation to count zeros and ones in binary representation of a number.
PHP
<?php function countSetBits( $n ) { // Base case: if n is 0, return 0 if ( $n == 0) return 0; else // If the last bit is set, add 1; otherwise, add 0 return ( $n & 1) + countSetBits( $n >> 1); } function countUnsetBits( $n ) { // Base case: if n is 0, return 0 if ( $n == 0) return 0; else // If the last bit is set, add 0; otherwise, add 1 return !( $n & 1) + countUnsetBits( $n >> 1); } // Get value from user $n = 99; // Function calling echo "Number of Ones in $n are: " .countSetBits( $n ). "\n" ; echo "Number of Zeroes in $n are: " .countUnsetBits( $n ). "\n" ; ?> |
Number of Ones in 99 are: 4 Number of Zeroes in 99 are: 3
Explanation
- The code uses a recursive approach to count the number of ones and zeroes in the binary representation of a given number $n.
- Both functions countSetBits and countUnsetBits return zero when $n is zero, as there are no bits to count.
- The code uses bitwise AND (`&`) and right shift (`>>`) operations to check the last bit of `$n`. If the last bit is set (i.e., it’s a one), the function increments the count of ones. If the last bit is not set (i.e., it’s a zero), the function increments the count of zeroes.
- The code demonstrates the usage of the two functions by calling them with a sample value of `$n` (99 in this case). It then prints the count of ones and zeroes in the binary representation of `$n`.
Contact Us