C program to count zeros and ones in binary representation of a number
Last Updated :
22 Dec, 2022
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
// C program for the above approach
#include <stdio.h>
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
// Initialise count variables
int count0 = 0, count1 = 0;
// Iterate through all the bits
while (N > 0) {
// If current bit is 1
if (N & 1) {
count1++;
}
// If current bit is 0
else {
count0++;
}
N = N >> 1;
}
// Print the count
printf("Count of 0s in N is %d\n", count0);
printf("Count of 1s in N is %d\n", count1);
}
// Driver Code
int main()
{
// Given Number
int N = 9;
// Function Call
count1s0s(N);
return 0;
}
OutputCount 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
// C program for the above approach
#include <math.h>
#include <stdio.h>
// Recursive approach to find the
// number of set bit in 1
int recursiveCount(int N)
{
// Base Case
if (N == 0) {
return 0;
}
// Return recursively
return (N & 1) + recursiveCount(N >> 1);
}
// Function to find 1s complement
int onesComplement(int n)
{
// Find number of bits in the
// given integer
int N = floor(log2(n)) + 1;
// XOR the given integer with
// pow(2, N) - 1
return ((1 << N) - 1) ^ n;
}
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
// Initialise the count variables
int count0, count1;
// Function call to find the number
// of set bits in N
count1 = recursiveCount(N);
// Function call to find 1s complement
N = onesComplement(N);
// Function call to find the number
// of set bits in 1s complement of N
count0 = recursiveCount(N);
// Print the count
printf("Count of 0s in N is %d\n", count0);
printf("Count of 1s in N is %d\n", count1);
}
// Driver Code
int main()
{
// Given Number
int N = 5;
// Function Call
count1s0s(N);
return 0;
}
OutputCount 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
// C program for the above approach
#include <math.h>
#include <stdio.h>
// Function to find 1s complement
int onesComplement(int n)
{
// Find number of bits in the
// given integer
int N = floor(log2(n)) + 1;
// XOR the given integer with
// pow(2, N) - 1
return ((1 << N) - 1) ^ n;
}
// Function to implement count of
// set bits using Brian Kernighan’s
// Algorithm
int countSetBits(int n)
{
// Initialise count
int count = 0;
// Iterate until n is 0
while (n) {
n &= (n - 1);
count++;
}
// Return the final count
return count;
}
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
// Initialise the count variables
int count0, count1;
// Function call to find the number
// of set bits in N
count1 = countSetBits(N);
// Function call to find 1s complement
N = onesComplement(N);
// Function call to find the number
// of set bits in 1s complement of N
count0 = countSetBits(N);
// Print the count
printf("Count of 0s in N is %d\n", count0);
printf("Count of 1s in N is %d\n", count1);
}
// Driver Code
int main()
{
// Given Number
int N = 5;
// Function Call
count1s0s(N);
return 0;
}
OutputCount of 0s in N is 1
Count of 1s in N is 2
Time Complexity: O(log N)
Auxiliary Space: O(1)
Similar Reads
C Program to count the number of zeros from 0 to N Given a number N, the task is to write C program to count the number of zeros from 0 to N. Examples: Input: N = 10 Output: 2 Explanation: The number with zeros are 0, 10 till 10. Hence the count of zeros is 2. Input: N = 20 Output: 3 Explanation: The number with zeros are 0, 10, 20 till 20. Hence th
2 min read
Convert Binary to Decimal in C In this article, we will learn how to write a C program to convert the given binary number into an equivalent decimal number. Binary numbers are expressed in base 2 ( 0, 1 ) and decimal numbers are expressed in base 10 ( 0-9 ).Algorithm to Convert Binary Numbers to DecimalThe idea is to extract the
3 min read
Count number of trailing zeros in Binary representation of a number using Bitset Given a number. The task is to count the number of Trailing Zero in Binary representation of a number using bitset.Examples: Input : N = 16Output : 4Binary representation of N is 10000. Therefore,number of zeroes at the end is 4.Input : N = 8Output : 3Approach: We simply set the number in the bitset
5 min read
Count of numbers in range [L, R] with LSB as 0 in their Binary representation Given two integers L and R. The task is to find the count of all numbers in the range [L, R] whose Least Significant Bit in binary representation is 0. Examples: Input: L = 10, R = 20 Output: 6 Input: L = 7, R = 11 Output: 2 Naive approach: The simplest approach is to solve this problem is to check
5 min read
Count trailing zeroes present in binary representation of a given number using XOR Given an integer N, the task is to find the number of trailing zeroes in the binary representation of the given number. Examples: Input: N = 12Output: 2Explanation:The binary representation of the number 13 is "1100".Therefore, there are two trailing zeros in the 12. Input: N = -56Output: 3Explanati
4 min read