Set all odd bits of a number | CPP code Set all odd bits of a number ; set all odd bit ; res for store 010101. . number ; generate number form of 010101. . ... till temp size ; if bit is odd , then generate number and or with res ; Driver code
#include <iostream> NEW_LINE using namespace std ; int oddbitsetnumber ( int n ) { int count = 0 ; int res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } int main ( ) { int n = 10 ; cout << oddbitsetnumber ( n ) ; return 0 ; }