C program to count zeros and ones in binary representation of a number - GeeksforGeeks

05 Aug, 2021
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 Nvoid 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 Codeint 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
// C program for the above approach#include <math.h>#include <stdio.h> // Recursive approach to find the// number of set bit in 1int recursiveCount(int N){     // Base Case    if (N == 0) {        return 0;    }     // Return recursively    return (N & 1) + recursiveCount(N >> 1);} // Function to find 1s complementint 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 Nvoid 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 Codeint 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 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 complementint 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// Algorithmint 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 Nvoid 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 Codeint 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)
kartikey134
gabaa406
base-conversion
binary-representation
setBitCount
Bit Magic
C Programs
School Programming
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Add two numbers without using arithmetic operators
Set, Clear and Toggle a given bit of a number in C
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Strings in C
Arrow operator -> in C/C++ with Examples
Header files in C/C++ and its uses
C Program to read contents of Whole File
Basics of File Handling in C