Open In App

Find position of left most dis-similar bit for two numbers

Last Updated : 23 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  1. Find bit length of n1 & n2.
  2. 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) )
  3. Take XOR of both number
  4. 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++




// C++ program to find the leftmost
// position of first dis-similar bit
#include <bits/stdc++.h>
using namespace std;
 
// Function to find first dis-similar bit
int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = floor(log2(n1)) + 1;
    int bitCount2 = floor(log2(n2)) + 1;
 
    // find bit difference and maxBit
    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;
}
 
// Driver program
int main()
{
    int n1 = 53, n2 = 55;
    cout << bitPos(n1, n2);
    return 0;
}


Java




// Java program to find the leftmost position of
// first dis-similar bit
 
import java.io.*;
 
class GFG {
         
// Function to find first dis-similar bit
static int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = (int)Math.floor(Math.log(n1) /
                                    Math.log(2)) + 1;
    int bitCount2 = (int)Math.floor(Math.log(n2) /
                                    Math.log(2)) + 1;
 
    // find bit difference and maxBit
    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;
}
 
// Driver Code
    public static void main (String[] args) {
 
        int n1 = 53, n2 = 55;
        System.out.println(bitPos(n1, n2));
}
}
// This code is contributed by ajit


Python3




# Python3 program to Find the leftmost
# position of first dis-similar bit
 
# from math lib import floor()
# and log2()
from math import floor, log2
 
# Function to find first
# dis-similar bit
def bitPos(n1, n2) :
     
    # return zero for equal number
    if n1 == n2 :
        return 0
     
    # find the 1st dis-similar bit
    # count bit length of n1 and n
    bitCount1 = floor(log2(n1)) + 1
    bitCount2 = floor(log2(n2)) + 1
     
    # find bit difference and maxBit
    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
     
# Driver code
if __name__ == "__main__" :
     
    n1, n2 = 53, 55
    print(bitPos(n1, n2))
     
# This code is contributed by Ryuga


C#




// C# to find the leftmost position of
// first dis-similar bit
using System;
 
class GFG
{
     
// Function to find first dis-similar bit
static int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = (int)Math.Floor(Math.Log(n1) /
                                    Math.Log(2)) + 1;
    int bitCount2 = (int)Math.Floor(Math.Log(n2) /
                                    Math.Log(2)) + 1;
 
    // find bit difference and maxBit
    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;
}
 
// Driver Code
public static void Main()
{
    int n1 = 53, n2 = 55;
    Console.Write(bitPos(n1, n2));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP




<?php
// PHP program to find the leftmost
// position of first dis-similar bit
 
// Function to find first dis-similar bit
function bitPos($n1, $n2)
{
    // return zero for equal number
    if ($n1 == $n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    $bitCount1 = floor(log($n1, 2)) + 1;
    $bitCount2 = floor(log($n2, 2)) + 1;
 
    // find bit difference and maxBit
    $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;
}
 
// Driver Code
$n1 = 53;
$n2 = 55;
echo bitPos($n1, $n2);
     
// This code is contributed by ajit
?>


Javascript




<script>
 
// Javascript program to find the leftmost
// position of first dis-similar bit
 
// Function to find first dis-similar bit
function bitPos(n1, n2)
{
     
    // Return zero for equal number
    if (n1 == n2)
        return 0;
 
    // Find the 1st dis-similar bit
    // count bit length of n1 and n2
    let bitCount1 = Math.floor(Math.log2(n1)) + 1;
    let bitCount2 = Math.floor(Math.log2(n2)) + 1;
 
    // find bit difference and maxBit
    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;
}
 
// Driver code
let n1 = 53, n2 = 55;
 
document.write(bitPos(n1, n2));
 
// This code is contributed by Manoj.
 
</script>


Output: 

5

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads