Find smallest number n such that n XOR n + 1 equals to given k . | CPP to find n such that XOR of n and n + 1 is equals to given n ; function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return -1 ; } int main ( ) { int k = 31 ; cout << xorCalc ( k ) ; return 0 ; }