Position of the K | Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code
< ? php function FindIndexKthBit ( $ n , $ k ) { $ cnt = 0 ; $ ind = 0 ; while ( $ n ) { if ( $ n & 1 ) $ cnt ++ ; if ( $ cnt == $ k ) return $ ind ; $ ind ++ ; $ n = $ n >> 1 ; } return -1 ; } $ n = 15 ; $ k = 3 ; $ ans = FindIndexKthBit ( $ n , $ k ) ; if ( $ ans != -1 ) echo $ ans ; else echo " No ▁ k - th ▁ set ▁ bit " ; ? >