Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | Java program to implement the above approach ; Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
class GFG { static int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) Math . log ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ( n & ( 1 << i ) ) <= 0 ) { m += ( 1 << i ) ; } } return m ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( equalXORandOR ( n ) ) ; } }