Check if a number has bits in alternate pattern | Set | function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; Driver Code
< ? php function allBitsAreSet ( $ n ) { if ( ( ( $ n + 1 ) & $ n ) == 0 ) return true ; return false ; } function bitsAreInAltOrder ( $ n ) { $ num = $ n ^ ( $ n >> 1 ) ; return allBitsAreSet ( $ num ) ; } $ n = 10 ; if ( bitsAreInAltOrder ( $ n ) ) echo " Yes " ; else echo " No " ; ? >