Count set bits in Bitwise XOR of all adjacent elements upto N | C ++ program to implement the above approach ; Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countXORSetBitsAdjElemRange1_N ( int N ) { int total_set_bits = 0 ; int bit_Position = 1 ; while ( N ) { total_set_bits += ( ( N + 1 ) / 2 * bit_Position ) ; N -= ( N + 1 ) / 2 ; bit_Position ++ ; } return total_set_bits ; } int main ( ) { int N = 4 ; cout << countXORSetBitsAdjElemRange1_N ( N ) ; return 0 ; }