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 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 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; } |
Count of 0s in N is 2 Count of 1s in N is 2
Time Complexity: O(log N)
Method 2 – Recursive Approach: The above approach can also be implemented using Recursion. Below is the implementation of the above approach:
// 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; } |
Count of 0s in N is 1 Count of 1s in N is 2
Time Complexity: O(log N)
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 untill 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 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 untill 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; } |
Count of 0s in N is 1 Count of 1s in N is 2
Time Complexity: O(log N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count number of trailing zeros in Binary representation of a number using Bitset
- Number of leading zeros in binary representation of a given number
- C Program to count the number of zeros from 0 to N
- Count of N-bit binary numbers without leading zeros
- Queries to answer the number of ones and zero to the left of given index
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Count numbers have all 1s together in binary representation
- Count of total bits toggled/flipped in binary representation of 0 to N
- Check if binary representation of a given number and its complement are anagram
- Largest number with binary representation is m 1's and m-1 0's
- Find the number obtained after concatenation of binary representation of M and N
- Comparing leading zeros in binary representations of two numbers
- Binary representation of a given number
- Check if binary representation of a number is palindrome
- Binary representation of next number
- Find the n-th number whose binary representation is a palindrome
- Binary representation of previous number
- Prime Number of Set Bits in Binary Representation | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

