Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) log2 ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ! ( n & ( 1 << i ) ) ) { m += ( 1 << i ) ; } } return m ; } int main ( ) { int n = 14 ; cout << equalXORandOR ( n ) ; return 0 ; }