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
< ? php function oddbitsetnumber ( $ n ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n $ res ) ; } $ n = 10 ; echo oddbitsetnumber ( $ n ) ; ? >