**Prerequisite:** Bitset function in STL library

Given a number **N**, the task is to find the absolute difference of the number of set and unset bits of this given number.

**Examples:**

Input:N = 14

Output:2

Explanation:

Binary representation of 14 is “1110”.

Here the number of set bits is 3 and the number of unset bits is 1.

Therefore, the absolute difference is 2.

Input:N = 56

Output:0

Explaination:

Binary representation of 56 is “110100”.

Here the number of set bits is 3 and the number of unset bits is 3.

Therefore, the absolute difference 0.

**Approach:**

- Count the total number of bits in the binary representation of the given number.
- Use bitset function defined in the STL library, to count the number of set bits efficiently.
- Then, we will subtract the set bits from the total number of bits to get the number of unset bits.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Max size of bitset ` `const` `int` `sz = 64; ` ` ` `// Function to return the total bits ` `// in the binary representation ` `// of a number ` `int` `totalbits(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(1 + log2(N)); ` `} ` ` ` `// Function to calculate the ` `// absolute difference ` `int` `absoluteDifference(` `int` `N) ` `{ ` ` ` `bitset<sz> arr(N); ` ` ` ` ` `int` `total_bits = totalbits(N); ` ` ` ` ` `// Calculate the number of ` ` ` `// set bits ` ` ` `int` `set_bits = arr.count(); ` ` ` ` ` `// Calculate the number of ` ` ` `// unset bits ` ` ` `int` `unset_bits = total_bits ` ` ` `- set_bits; ` ` ` ` ` `int` `ans = ` `abs` `(set_bits ` ` ` `- unset_bits); ` ` ` ` ` `// Return the absolute difference ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 14; ` ` ` ` ` `// Function Call ` ` ` `cout << absoluteDifference(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

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 of numbers having only one unset bit in a range [L,R]
- Set the rightmost unset bit
- Set the Left most unset bit
- Check whether the bit at given position is set or unset
- Set the rightmost unset bit
- Get the position of rightmost unset bit
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Find the largest number with n set and m unset bits
- Find the smallest number with n set and m unset bits
- Check if a number has same number of set and unset bits
- Count unset bits of a number
- Count unset bits in a range
- Python | Count unset bits in a range
- Count total unset bits in all the numbers from 1 to N
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Inserting m into n such that m starts at bit j and ends at bit i.

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.