Find position of left most dis-similar bit for two numbers
Last Updated :
23 Jun, 2022
Given two numbers n1 and n2. The task is to find the position of first mismatching bit in the binary representation of the two numbers from left. We need to find this bit after making lengths of binary representations of both numbers same. We make lengths same by appending 0s in the smaller number.
Note:
- For Example: n1 = 1, n2 = 7. Binary representation of n1 and n2 will be “1” and “111” respectively. Append two zeros to n1 to make it “100”.
- Print zero if n1 is equal to n2.
Examples:
Input: n1 = 12, n2 = 34
Output: 2
Binary representation of 12 is 1100 and of 34 is 100010.
First make both representations of the
same length by appending 0s.
So the first representation now becomes 11000.
The second bit is the different bit.
Input: n1 = 1, n2 = 2
Output: 2
For finding the position of leftmost dis-similar bit among bit representation of two numbers, either bit by bit comparison can be done or a derived formula can be used. Although the time complexity for both is same.
For finding the left most dis-similar bit first of all equalize the bit length of both numbers by multiply the smaller one with pow(2, bit-length Difference). After making bit-length equal take XOR of both number. Now, leftmost dis-similar bit is clearly reflected in XOR value. Subtracting the bit-length of XOR value from bit-length of given number plus 1 is the position of leftmost dis-similar bit can be concluded.
Algorithm:
- Find bit length of n1 & n2.
- Equalize bit-length of both number by placing zero to right of smaller number (same as multiplying smaller one by pow(2, bit-length Difference) )
- Take XOR of both number
- Difference of bit-length of any number and bit-length of XOR value is required answer plus 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int bitPos( int n1, int n2)
{
if (n1 == n2)
return 0;
int bitCount1 = floor (log2(n1)) + 1;
int bitCount2 = floor (log2(n2)) + 1;
int bitDiff = abs (bitCount1 - bitCount2);
int maxBitCount = max(bitCount1, bitCount2);
if (bitCount1 > bitCount2) {
n2 = n2 * pow (2, bitDiff);
}
else {
n1 = n1 * pow (2, bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue = floor (log2(xorValue)) + 1;
int disSimilarBitPosition = maxBitCount - bitCountXorValue + 1;
return disSimilarBitPosition;
}
int main()
{
int n1 = 53, n2 = 55;
cout << bitPos(n1, n2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int bitPos( int n1, int n2)
{
if (n1 == n2)
return 0 ;
/** find the 1st dis-similar bit **/
int bitCount1 = ( int )Math.floor(Math.log(n1) /
Math.log( 2 )) + 1 ;
int bitCount2 = ( int )Math.floor(Math.log(n2) /
Math.log( 2 )) + 1 ;
int bitDiff = Math.abs(bitCount1 - bitCount2);
int maxBitCount = Math.max(bitCount1,
bitCount2);
if (bitCount1 > bitCount2)
{
n2 = n2 * ( int )Math.pow( 2 , bitDiff);
}
else
{
n1 = n1 * ( int )Math.pow( 2 , bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue = ( int )Math.floor(Math.log(xorValue) /
Math.log( 2 )) + 1 ;
int disSimilarBitPosition = maxBitCount -
bitCountXorValue + 1 ;
return disSimilarBitPosition;
}
public static void main (String[] args) {
int n1 = 53 , n2 = 55 ;
System.out.println(bitPos(n1, n2));
}
}
|
Python3
from math import floor, log2
def bitPos(n1, n2) :
if n1 = = n2 :
return 0
bitCount1 = floor(log2(n1)) + 1
bitCount2 = floor(log2(n2)) + 1
bitDiff = abs (bitCount1 - bitCount2)
maxBitCount = max (bitCount1, bitCount2)
if (bitCount1 > bitCount2) :
n2 * = pow ( 2 , bitDiff)
else :
n1 * = pow ( 2 , bitDiff)
xorValue = n1 ^ n2
bitCountXorValue = floor(log2(xorValue)) + 1
disSimilarBitPosition = (maxBitCount -
bitCountXorValue + 1 )
return disSimilarBitPosition
if __name__ = = "__main__" :
n1, n2 = 53 , 55
print (bitPos(n1, n2))
|
C#
using System;
class GFG
{
static int bitPos( int n1, int n2)
{
if (n1 == n2)
return 0;
int bitCount1 = ( int )Math.Floor(Math.Log(n1) /
Math.Log(2)) + 1;
int bitCount2 = ( int )Math.Floor(Math.Log(n2) /
Math.Log(2)) + 1;
int bitDiff = Math.Abs(bitCount1 - bitCount2);
int maxBitCount = Math.Max(bitCount1,
bitCount2);
if (bitCount1 > bitCount2)
{
n2 = n2 * ( int )Math.Pow(2, bitDiff);
}
else
{
n1 = n1 * ( int )Math.Pow(2, bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue = ( int )Math.Floor(Math.Log(xorValue) /
Math.Log(2)) + 1;
int disSimilarBitPosition = maxBitCount -
bitCountXorValue + 1;
return disSimilarBitPosition;
}
public static void Main()
{
int n1 = 53, n2 = 55;
Console.Write(bitPos(n1, n2));
}
}
|
PHP
<?php
function bitPos( $n1 , $n2 )
{
if ( $n1 == $n2 )
return 0;
$bitCount1 = floor (log( $n1 , 2)) + 1;
$bitCount2 = floor (log( $n2 , 2)) + 1;
$bitDiff = abs ( $bitCount1 - $bitCount2 );
$maxBitCount = max( $bitCount1 , $bitCount2 );
if ( $bitCount1 > $bitCount2 )
{
$n2 = $n2 * pow(2, $bitDiff );
}
else {
$n1 = $n1 * pow(2, $bitDiff );
}
$xorValue = $n1 ^ $n2 ;
$bitCountXorValue = floor (log( $xorValue , 2)) + 1;
$disSimilarBitPosition = $maxBitCount -
$bitCountXorValue + 1;
return $disSimilarBitPosition ;
}
$n1 = 53;
$n2 = 55;
echo bitPos( $n1 , $n2 );
?>
|
Javascript
<script>
function bitPos(n1, n2)
{
if (n1 == n2)
return 0;
let bitCount1 = Math.floor(Math.log2(n1)) + 1;
let bitCount2 = Math.floor(Math.log2(n2)) + 1;
let bitDiff = Math.abs(bitCount1 - bitCount2);
let maxBitCount = Math.max(bitCount1, bitCount2);
if (bitCount1 > bitCount2)
{
n2 = n2 * Math.pow(2, bitDiff);
}
else
{
n1 = n1 * Math.pow(2, bitDiff);
}
let xorValue = n1 ^ n2;
let bitCountXorValue = Math.floor(
Math.log2(xorValue)) + 1;
let disSimilarBitPosition = maxBitCount -
bitCountXorValue + 1;
return disSimilarBitPosition;
}
let n1 = 53, n2 = 55;
document.write(bitPos(n1, n2));
</script>
|
Share your thoughts in the comments
Please Login to comment...