# Absolute difference between set and unset bit count in N in C++

In this article, we will discuss how to find the absolute difference between set and unset bit count in N in C++ with the help of an example.

The task involves determining the absolute difference between an integer number's set (or bits with value 1) and unset (or bits with value 0) bits. We must first determine the absolute difference between the two counts of set and unset bits in the binary representation of the integer. Let us look at pseudocode for the following example.

### Program:

Output:

Explanation:

• In this example, the absVal function is a function that provides the absolute value of a given number. If the number is not negative, it returns the number; if it is, it returns the reverse of that number.
• Given an integer n as input, the bitDiff function calculates the absolute difference between a set and an unset bit.
• Initialise two variables, set and unSet, to keep track of the number of set and unset bits, respectively. Also, create a 'x' variable and set its value to the input number.
• Use a while loop to run through each bit of x. The loop continues until x is equal to zero.
• Find the least significant bit (LSB) of x by using the bitwise AND operation with 1 (n & 1).
• If the bit is set and the LSB is 1, increase set by 1. If not, add one to unSet.
• Shift x to the right by 1 to move on to the next section for the subsequent iteration.
• Use the absVal function to obtain the absolute difference between set and unSet after the loop. Put the outcome in the res variable.
• At the conclusion, print the original number along with the calculated bit difference.