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 function
function allBitsAreSet ( n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } function bitsAreInAltOrder ( n ) { let num = n ^ ( n >>> 1 ) ; return allBitsAreSet ( num ) ; } let n = 10 ; if ( bitsAreInAltOrder ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;