Largest number M having bit count of N such that difference between their OR and XOR value is maximized | C ++ program for the above approach ; Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxORminusXOR ( int N ) { int MSB = log2 ( N ) ; int M = 0 ; for ( int i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } int main ( ) { int N = 10 ; cout << maxORminusXOR ( N ) ; return 0 ; }