Find profession in a special family | Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code
function countSetBits ( n ) { let count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function findProffesion ( level , pos ) { let c = countSetBits ( pos - 1 ) ; return ( c % 2 != 0 ) ? ' ' : ' ' ; } let level = 3 , pos = 4 ; let prof = ( findProffesion ( level , pos ) == ' ' ) ? " " : " " ; document . write ( prof ) ; }