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

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. Bitwise representation of n1 and n4 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 representaion 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

$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
?>

Output:

5


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Akanksha_Rai, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.