Bitwise OR ( | ) of all even number from 1 to N | C ++ implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = log2 ( n ) + 1 ; return pow ( 2 , bitCount ) - 2 ; } int main ( ) { int n = 10 ; cout << bitwiseOrTillN ( n ) ; return 0 ; }