C program to count zeros and ones in binary representation of a number
Given a number N, the task is to write C program to count the number of 0s and 1s in the binary representation of N.
Examples:
Input: N = 5
Output:
Count of 0s: 1
Count of 1s: 2
Explanation: Binary representation of 5 is “101”.
Input: N = 22
Output:
Count of 0s: 2
Count of 1s: 3
Explanation: Binary representation of 22 is “10110”.
Method 1 – Naive Approach: The idea is to iterate through all bits in the binary representation of N and increment the count of 0s if current bit is ‘0’ else increment the count of 1s.
Below is the implementation of the above approach:
C
#include <stdio.h>
void count1s0s( int N)
{
int count0 = 0, count1 = 0;
while (N > 0) {
if (N & 1) {
count1++;
}
else {
count0++;
}
N = N >> 1;
}
printf ( "Count of 0s in N is %d\n" , count0);
printf ( "Count of 1s in N is %d\n" , count1);
}
int main()
{
int N = 9;
count1s0s(N);
return 0;
}
|
Output
Count of 0s in N is 2
Count of 1s in N is 2
Time Complexity: O(log N)
Auxiliary Space: O(1)
Method 2 – Recursive Approach: The above approach can also be implemented using Recursion.
Below is the implementation of the above approach:
C
#include <math.h>
#include <stdio.h>
int recursiveCount( int N)
{
if (N == 0) {
return 0;
}
return (N & 1) + recursiveCount(N >> 1);
}
int onesComplement( int n)
{
int N = floor (log2(n)) + 1;
return ((1 << N) - 1) ^ n;
}
void count1s0s( int N)
{
int count0, count1;
count1 = recursiveCount(N);
N = onesComplement(N);
count0 = recursiveCount(N);
printf ( "Count of 0s in N is %d\n" , count0);
printf ( "Count of 1s in N is %d\n" , count1);
}
int main()
{
int N = 5;
count1s0s(N);
return 0;
}
|
Output
Count of 0s in N is 1
Count of 1s in N is 2
Time Complexity: O(log N)
Auxiliary Space: O(1)
Method 3 – Using Brian Kernighan’s Algorithm
We can find the count of set bits using the steps below:
- Initialise count to 0.
- If N > 0, then update N as N & (N – 1) as this will unset the most set bit from the right as shown below:
if N = 10;
Binary representation of N = 1010
Binary representation of N - 1 = 1001
-------------------------------------
Logical AND of N and N - 1 = 1000
- Increment the count for the above steps and repeat the above steps until N becomes 0.
To find the count of 0s in the binary representation of N, find the one’s complement of N and find the count of set bits using the approach discussed above.
Below is the implementation of the above approach:
C
#include <math.h>
#include <stdio.h>
int onesComplement( int n)
{
int N = floor (log2(n)) + 1;
return ((1 << N) - 1) ^ n;
}
int countSetBits( int n)
{
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
void count1s0s( int N)
{
int count0, count1;
count1 = countSetBits(N);
N = onesComplement(N);
count0 = countSetBits(N);
printf ( "Count of 0s in N is %d\n" , count0);
printf ( "Count of 1s in N is %d\n" , count1);
}
int main()
{
int N = 5;
count1s0s(N);
return 0;
}
|
Output
Count of 0s in N is 1
Count of 1s in N is 2
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...