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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int X, int Y)
{
int ans = 0;
while (X != 0 || Y != 0) {
if (X % 2 == Y % 2)
ans++;
X >>= 1;
Y >>= 1;
}
return ans;
}
int main()
{
int X = 10, Y = 6;
cout << solve(X, Y);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int solve( int X, int Y)
{
int ans = 0 ;
while (X != 0 || Y != 0 )
{
if (X % 2 == Y % 2 )
ans++;
X >>= 1 ;
Y >>= 1 ;
}
return ans;
}
public static void main(String[] args)
{
int X = 10 , Y = 6 ;
System.out.print(solve(X, Y));
}
}
|
Python3
def solve(X, Y):
ans = 0
while (X ! = 0 or Y ! = 0 ):
if X % 2 = = Y % 2 :
ans + = 1
X >> = 1
Y >> = 1
return ans
X = 10
Y = 6
print (solve(X, Y))
|
C#
using System;
public class GFG{
public static int solve( int X, int Y)
{
int ans = 0;
while (X != 0 || Y != 0)
{
if (X % 2 == Y % 2)
ans++;
X >>= 1;
Y >>= 1;
}
return ans;
}
static public void Main (){
int X = 10, Y = 6;
Console.Write(solve(X, Y));
}
}
|
Javascript
<script>
function solve(X, Y)
{
var ans = 0;
while (X != 0 || Y != 0)
{
if (X % 2 == Y % 2)
ans++;
X >>= 1;
Y >>= 1;
}
return ans;
}
var X = 10;
var Y = 6;
document.write(solve(X, Y));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int countUnmatchedBits( int A, int B)
{
int XOR = (A ^ B);
int totalBits = ( int )log2(XOR) + 1;
int setCount = 0;
while (XOR) {
XOR = XOR & (XOR - 1);
setCount++;
}
return totalBits - setCount;
}
int main()
{
int A = 10, B = 6;
int result = countUnmatchedBits(A, B);
cout << "Number of matching bits : " << result;
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int countUnmatchedBits( int A, int B)
{
int XOR = (A ^ B);
int totalBits = ( int )Math.floor(Math.log(XOR)/Math.log( 2 )) + 1 ;
int setCount = 0 ;
while (XOR > 0 ) {
XOR = XOR & (XOR - 1 );
setCount++;
}
return totalBits - setCount;
}
public static void main(String args[])
{
int A = 10 , B = 6 ;
int result = countUnmatchedBits(A, B);
System.out.println( "Number of matching bits : " + result);
}
}
|
Python
import math
def countUnmatchedBits(A, B):
XOR = (A ^ B)
totalBits = int (math.floor(math.log(XOR) / math.log( 2 )) + 1 )
setCount = 0
while XOR > 0 :
XOR = XOR & (XOR - 1 )
setCount + = 1
return totalBits - setCount
A, B = 10 , 6
result = countUnmatchedBits(A, B)
print 'Number of matching bits : ' , result
|
C#
using System;
class GFG {
static int countUnmatchedBits( int A, int B)
{
int XOR = (A ^ B);
int totalBits
= ( int )Math.Floor(Math.Log(XOR) / Math.Log(2))
+ 1;
int setCount = 0;
while (XOR > 0) {
XOR = XOR & (XOR - 1);
setCount++;
}
return totalBits - setCount;
}
public static void Main( string [] args)
{
int A = 10, B = 6;
int result = countUnmatchedBits(A, B);
Console.WriteLine( "Number of matching bits : "
+ result);
}
}
|
Javascript
function countUnmatchedBits(A, B)
{
let XOR = (A ^ B);
let totalBits = Math.floor(Math.log2(XOR)) + 1;
let setCount = 0;
while (XOR) {
XOR = XOR & (XOR - 1);
setCount++;
}
return totalBits - setCount;
}
let A = 10;
let B = 6;
let result = countUnmatchedBits(A, B);
console.log( "Number of matching bits :" , result);
|
OutputNumber of matching bits : 2
Time Complexity: O(1)
Auxiliary Space: O(1)