Check whether bits are in alternate pattern in the given range | function to check whether bits are in alternate pattern in the given range ; right shift n by ( l - 1 ) bits ; get the bit at the last position in ' num ' ; right shift ' num ' by 1 ; loop until there are bits in the given range ; get the bit at the last position in ' num ' ; if true , then bits are not in alternate pattern ; update ' prev ' ; right shift ' num ' by 1 ; bits are in alternate pattern in the given range ; Driver Code
< ? php function bitsAreInAltPatrnInGivenTRange ( $ n , $ l , $ r ) { $ num = $ n >> ( $ l - 1 ) ; $ prev = $ num & 1 ; $ num = $ num >> 1 ; for ( $ i = 1 ; $ i <= ( $ r - $ l ) ; $ i ++ ) { $ curr = $ num & 1 ; if ( $ curr == $ prev ) return false ; $ prev = $ curr ; $ num = $ num >> 1 ; } return true ; } $ n = 18 ; $ l = 1 ; $ r = 3 ; if ( bitsAreInAltPatrnInGivenTRange ( $ n , $ l , $ r ) ) echo " Yes " ; else echo " No " ; ? >