The method should return a number which represents a hex value for that number of bits set.
The problem is ambiguous as stated. Many numbers have the same number of bits set.
Example: If I pass 4 as parameter then the function should return 17 because for bits set to true (1111) = 17.
No it isn't, it's 15, but 30 would also satisfy the requirement (11110), and so would 60, 120, ..., and also various numbers of binary form 10101010
, 01010101
, 110011
, etc.
Similarly, when I pass 5 as parameter then the function should return 37 as (11111) = 37.
No it doesn't. 11111 is 31.
77 for 6 as parameter
Wrong again, it's 63.
and so on.
The problem is ambiguous as stated. Either you've left something out or it needs to be clarified at source.
And you haven't got one single binary->decimal conversion right. You need to review that. Or are you talking in octal?