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 = 5Output:

Count of 0s: 1

Count of 1s: 2Explanation:Binary representation of 5 is “101”.Input:N = 22Output:

Count of 0s: 2

Count of 1s: 3Explanation: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;` `}` |

*chevron_right*

*filter_none*

**Output**

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 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;` `}` |

*chevron_right*

*filter_none*

**Output**

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

`// 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;` `}` |

*chevron_right*

*filter_none*

**Output**

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.