For two integers X and Y are the tasks is to find the number of bits that are the same in their binary representation by not considering any leading zeroes after the leftmost set bit of the greater number in binary form.
Examples:
Input: X = 10, Y = 6
Output: 2
Explanation: The binary representation of 10 is 1010 and 6 is 0110. So, the number of same bits are 2.Input : X = 3, Y = 16
Output : 2
Approach:
Intuition:
Calculate least significant bit (LSB) by using (i % 2), where i is any integer and Check if the least significant bit (LSB) of the given integer X, Y are same or not. If same then, increment the answer and right shift the both integer by 1 for checking the LSB are same or not for next bit.
Algorithm:
- Keep doing the following steps until either X or Y is not 0.
- Calculate the LSB of given integer X and Y by X % 2 and Y % 2 and check if both are same or not.
- If same, then increment the answer
- Right shift the both given integer by 1
- Calculate the LSB of given integer X and Y by X % 2 and Y % 2 and check if both are same or not.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to find the number of same bit between // two integer. int solve( int X, int Y)
{ int ans = 0;
while (X != 0 || Y != 0) {
// Check if both LSB are same or not
if (X % 2 == Y % 2)
ans++;
// Right shift the both given integer by 1
X >>= 1;
Y >>= 1;
}
return ans;
} // Driver code int main()
{ int X = 10, Y = 6;
// Find number of same bits
cout << solve(X, Y);
return 0;
} |
// Java implementation of the approach import java.io.*;
class GFG
{ // Function to find the number of same bit between
// two integer.
public static int solve( int X, int Y)
{
int ans = 0 ;
while (X != 0 || Y != 0 )
{
// Check if both LSB are same or not
if (X % 2 == Y % 2 )
ans++;
// Right shift the both given integer by 1
X >>= 1 ;
Y >>= 1 ;
}
return ans;
}
public static void main(String[] args)
{
int X = 10 , Y = 6 ;
// Find number of same bits
System.out.print(solve(X, Y));
}
} // This code is contributed by Rohit Pradhan |
# Python3 implementation of the approach # Function to find the number of same bit between # two integer. def solve(X, Y):
ans = 0
while (X ! = 0 or Y ! = 0 ):
# Check if both LSB are same or not
if X % 2 = = Y % 2 :
ans + = 1
# Right shift the both given integer by 1
X >> = 1
Y >> = 1
return ans
# Driver code X = 10
Y = 6
# function call print (solve(X, Y))
# This code is contributed by phasing17 |
// C# implementation of the approach using System;
public class GFG{
// Function to find the number of same bit between
// two integer.
public static int solve( int X, int Y)
{
int ans = 0;
while (X != 0 || Y != 0)
{
// Check if both LSB are same or not
if (X % 2 == Y % 2)
ans++;
// Right shift the both given integer by 1
X >>= 1;
Y >>= 1;
}
return ans;
}
static public void Main (){
int X = 10, Y = 6;
// Find number of same bits
Console.Write(solve(X, Y));
}
} // This code is contributed by hrithikgarg03188. |
<script> // JS implementation of the approach // Function to find the number of same bit between // two integer. function solve(X, Y)
{ var ans = 0;
while (X != 0 || Y != 0)
{
// Check if both LSB are same or not
if (X % 2 == Y % 2)
ans++;
// Right shift the both given integer by 1
X >>= 1;
Y >>= 1;
}
return ans;
} // Driver code var X = 10;
var Y = 6;
// Find number of same bits document.write(solve(X, Y)); // This code is contributed by phasing17 </script> |
2
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Approach: Counting unset bits in XOR of the numbers
The XOR of two bits is 1/set if they are unmatched (ie, one is unset and the other is set), and the XOR of two bits is 0/unset if they are matched (ie both are set or unset).
Therefore, the total number of matching bits in two numbers is equal to the number of unmatched bits in their XOR, or the difference between total bits and unmatched bits in their XOR.
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// compute number of matching bits int countUnmatchedBits( int A, int B)
{ //calculate XOR
int XOR = (A ^ B);
//count total number of bits using log2(XOR) + 1
int totalBits = ( int )log2(XOR) + 1;
// Check for 1's in the binary form using
// Brian Kerninghan's Algorithm
int setCount = 0;
while (XOR) {
XOR = XOR & (XOR - 1);
setCount++;
}
//the number of unset bits is equal to
//the difference between total bits
//and the set bits count
return totalBits - setCount;
} // Driver code int main()
{ int A = 10, B = 6;
int result = countUnmatchedBits(A, B);
cout << "Number of matching bits : " << result;
return 0;
} //this code is contributed by phasing17 |
import java.util.*;
import java.io.*;
// Java program for the above approach class GFG{
// compute number of matching bits
static int countUnmatchedBits( int A, int B)
{
//calculate XOR
int XOR = (A ^ B);
//count total number of bits using log2(XOR) + 1
int totalBits = ( int )Math.floor(Math.log(XOR)/Math.log( 2 )) + 1 ;
// Check for 1's in the binary form using
// Brian Kerninghan's Algorithm
int setCount = 0 ;
while (XOR > 0 ) {
XOR = XOR & (XOR - 1 );
setCount++;
}
//the number of unset bits is equal to
//the difference between total bits
//and the set bits count
return totalBits - setCount;
}
// Driver code
public static void main(String args[])
{
int A = 10 , B = 6 ;
int result = countUnmatchedBits(A, B);
System.out.println( "Number of matching bits : " + result);
}
} // This code is contributed by subhamgoyal2014. |
# Python program for the above approach import math
# compute number of matching bits def countUnmatchedBits(A, B):
# calculate XOR
XOR = (A ^ B)
# count total number of bits using log2(XOR) + 1
totalBits = int (math.floor(math.log(XOR) / math.log( 2 )) + 1 )
# Check for 1's in the binary form using Brian Kerninghan's Algorithm
setCount = 0
while XOR > 0 :
XOR = XOR & (XOR - 1 )
setCount + = 1
# the number of unset bits is equal to the difference between
# total bits and the set bits count
return totalBits - setCount
A, B = 10 , 6
result = countUnmatchedBits(A, B)
print 'Number of matching bits : ' , result
# This code is contributed by KaaL-EL. |
// C# program for the above approach using System;
class GFG {
// compute number of matching bits
static int countUnmatchedBits( int A, int B)
{
// calculate XOR
int XOR = (A ^ B);
// count total number of bits using log2(XOR) + 1
int totalBits
= ( int )Math.Floor(Math.Log(XOR) / Math.Log(2))
+ 1;
// Check for 1's in the binary form using
// Brian Kerninghan's Algorithm
int setCount = 0;
while (XOR > 0) {
XOR = XOR & (XOR - 1);
setCount++;
}
// the number of unset bits is equal to
// the difference between total bits
// and the set bits count
return totalBits - setCount;
}
// Driver code
public static void Main( string [] args)
{
int A = 10, B = 6;
// Function call
int result = countUnmatchedBits(A, B);
Console.WriteLine( "Number of matching bits : "
+ result);
}
} // This code is contributed by phasing17 |
// JavaScript implementation of the approach // compute number of matching bits function countUnmatchedBits(A, B)
{ // calculate XOR
let XOR = (A ^ B);
// count total number of bits using log2(XOR) + 1
let totalBits = Math.floor(Math.log2(XOR)) + 1;
// Check for 1's in the binary form using
// Brian Kerninghan's Algorithm
let setCount = 0;
while (XOR) {
XOR = XOR & (XOR - 1);
setCount++;
}
// the number of unset bits is equal to
// the difference between total bits
// and the set bits count
return totalBits - setCount;
} // Driver code let A = 10; let B = 6; // Function Call let result = countUnmatchedBits(A, B); console.log( "Number of matching bits :" , result);
// this code is contributed by phasing17 |
Number of matching bits : 2
Time Complexity: O(1)
Auxiliary Space: O(1)