Set all even bits of a number | Simple CPP program to set all even bits of a number ; Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ;
#include <iostream> NEW_LINE using namespace std ; int evenbitsetnumber ( int n ) { int count = 0 , res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } / * Driver code * int main ( ) { int n = 10 ; cout << evenbitsetnumber ( n ) ; return 0 ; }