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.
Pseudocode:
Example
Function abseValue(num):
if num < 0:
return -num
else:
return num
Function bitDifference(num):
setBit = 0
unSetBit = 0
n = num
while n is not equal to 0:
if n AND 1 is equal to 1:
Increment setBit by 1
else:
Increment unSetBit by 1
Right shift n by 1
result = abseValue(setBit - unSetBit)
Print "Given number: " + num
Print "Bit difference: " + result
Program:
Example
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program to Find the absolute difference between the set and unset bit of a number
*/
class BinaryBits
{
public: int abseValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void bitDifference(int num)
{
int setBit = 0;
int unSetBit = 0;
int n = num;
while (n != 0)
{
if ((n &1) == 1)
{
// Count the set bit
setBit += 1;
}
else
{
// Count the unset bit
unSetBit += 1;
}
// Shift n value by 1 to right
n = (n >> 1);
}
// Calculate the bit difference
int result = this->abseValue(setBit - unSetBit);
// Display given number
cout << " Given number : " << num << endl;
// Display calculated result
cout << " Bit different : " << result << endl;
}
};
int main()
{
BinaryBits *task = new BinaryBits();
// Test A
// num = 536 binary (1000011000)
// 1000011000
// - --
// set bit = 3
//
// 1000011000
// ---- ---
// unset bit = 7
// different = (3 - 7) = -4 = abs(4)
// -----------------------------------
// result = 4
task->bitDifference(536);
// Test B
// num = 143 binary (10001111)
// 10001111
// - ----
// set bit = 5
//
// 10001111
// ---
// unset bit = 3
// different = (5 - 2) = 3
// -----------------------------------
// result = 3
task->bitDifference(143);
// Test C
// num = 231 binary (11100111)
// 11100111
// --- ---
// set bit = 6
//
// 11100111
// --
// unset bit = 2
// different = (6 - 2) = 4
// -----------------------------------
// result = 4
task->bitDifference(231);
return 0;
}
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.