First number to leave an odd remainder after repetitive division by 2 | C ++ Program to implement the above approach ; Function to return the position least significant set bit ; Function return the first number to be converted to an odd integer ; Stores the positions of the first set bit ; If both are same ; If A has the least significant set bit ; Otherwise ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstSetBitPos ( int n ) { return log2 ( n & - n ) + 1 ; } int oddFirst ( int a , int b ) { int steps_a = getFirstSetBitPos ( a ) ; int steps_b = getFirstSetBitPos ( b ) ; if ( steps_a == steps_b ) { return -1 ; } if ( steps_a > steps_b ) { return b ; } if ( steps_a < steps_b ) { return a ; } } int main ( ) { int a = 10 ; int b = 8 ; cout << oddFirst ( a , b ) ; }