How to turn on a particular bit in a number ? | Returns a number that has all bits same as n except the k 'th bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver Code
< ? php function turnOnK ( $ n , $ k ) { if ( $ k <= 0 ) return $ n ; return ( $ n | ( 1 << ( $ k - 1 ) ) ) ; } $ n = 4 ; $ k = 2 ; echo turnOnK ( $ n , $ k ) ; ? >