Find position of left most dis | C ++ program to find the leftmost position of first dis - similar bit ; Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver program
#include <bits/stdc++.h> NEW_LINE 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 ; }