Position of rightmost different bit

Problem Link

When we do XOR we will get 1 in all places where two bits are different.
Then we can find the position of rightmost set bit.

Code:

package bitManipulation;

public class RightMostDifferentBit {

    public static void main(String[] args){
        int m = 11, n = 9;
       
        int diffValue = m ^ n;
       
        int result = (int)(Math.log10( diffValue & -diffValue) / Math.log10(2)) +1;
       
        System.out.print(result);
       
    }
}


Output:

2
 

Comments

Popular posts from this blog

Rearrange Array in Maximum-Minimum form

Second Largest Element

Check if a number is a power of another number