Alternate bits of two numbers to create a new number | set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1
< ? php function setevenbits ( $ n ) { $ temp = $ n ; $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 1 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n & $ res ) ; } function setoddbits ( $ m ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ m ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ m & $ res ) ; } function getAlternateBits ( $ n , $ m ) { $ tempn = setevenbits ( $ n ) ; $ tempm = setoddbits ( $ m ) ; return ( $ tempn $ tempm ) ; } $ n = 10 ; $ m = 11 ; echo getAlternateBits ( $ n , $ m ) ; ? >