Number of mismatching bits in the binary representation of two integers


Binary language is the language of the computers. So, every integer, character or symbols needs to be converted into binary system and vice versa. There is binary representation to all high-level language. In this article, we will discuss about the binary representation of integers. Also, we will find the mismatching bits in the binary representation of two integers.

Input Output Scenarios

We are given two integers X and Y. The number of mismatching bits in their binary representation is the output.

Input: X = 25, Y = 15
Output: 3
Input: X = 6, X = 19
Output: 3

For X = 25, Y = 15, binary representation of 25 is 11001 and that of 15 is 1111. The number mismatching bits are 3.

Using for Loop and Shifting Positions

We will use the for loop to iterate through the bits of the integers. The loop will iterate from 0 to 32 because in modern computers int data type is represented using 32 bits.

In the for loop, we will check the bits at all positions of both the integers and find whether the bit at the ith position is same or different. If it is different, we will increase the count variable by one.

Example

Following example calculates the number of mismatching bits in the binary representation of two integers −

#include <iostream>
using namespace std;
int number(int A, int B){
   int count = 0;
   for (int i = 0; i < 32; i++) {
      if ((A & (1 << i)) != (B & (1 << i))) {
         count++;
      }
   }
   return count;
}
int main(){
   int A = 6, B = 19;
   cout<< "Number of mismatching bits in binary representation of "<< A << " and " << B << " is " << number(A, B) << std::endl;
   return 0;
}

Output

Number of mismatching bits in binary representation of 6 and 19 is 3

Using XOR Operation

We will use the XOR operation on the two integers. The XOR operation checks whether the bits of the integers at the same position is different or not. If it is same, the result is 0. Whereas if it is different, the result is 1. This result is stored in the x variable. This will store all the mismatching bits of the two integers (as 1).

Next, we run a while loop until the value of x is non-zero. In the loop, we use the AND operator to check if the most significant bit (generally the farthest to the left) is 1 or not. If it is 1, the count variable is increased by one.

Then, we shift the most significant bit to the right by one bit using (x >>= 1). This enables us to calculate the total number of mismatching bits.

Example

Let us see an example −

#include <iostream>
using namespace std;
int number(int A, int B){
   int x = A ^ B;
   int count = 0;
   while(x > 0){
      if(x & 1){
         count++;
      }
      x >>= 1;
   }
   return count;
}
int main(){
   int A = 116, B = 219;
   cout<< "Number of mismatching bits in binary representation of "<< A << " and " << B << " is " << number(A, B) << std::endl;
   return 0;
}

Output

Number of mismatching bits in binary representation of 116 and 219 is 6

Using the Bitset Class

We will use the bitset class of the Standard C++ library. It is used to create an object which contains the binary representation of the specified integer.

std::bitset<32> name(integer);

Next, we perform XOR operation of both the objects. This operation gives the bits which are different in the binary representation of both the integers. Then, we use count() function to calculate the total number of mismatching bits.

Example

Following example calculates the number of mismatching bits using the bitset class −

#include <iostream>
#include <bitset>
using namespace std;
int number(int A, int B){
   std::bitset<32> num1(A);
   std::bitset<32> num2(B);
   std::bitset<32> result = num1 ^ num2;
   return result.count();
}
int main(){
   int A = 89, B = 72;
   cout<< "Number of mismatching bits in binary representation of "<< A << " and " << B << " is " << number(A, B) << std::endl;
   return 0;
}

Output

Number of mismatching bits in binary representation of 89 and 72 is 2

Conclusion

We have discussed different ways for finding the number of mismatching bits in the binary representation of two integers. The first approach is to use the for loop to iterate through the bits of the integers. The second approach is to use the XOR operation on the integers. The third approach is to use the bitset class of the C++ library.

Updated on: 05-Jan-2024

22 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements