For any positive 32-bit integer (1 ≤ n ≤ 0xFFFFFFFF
) output the number of bits needed to represent that integer.
Test cases
| n | n in binary | bits needed |
|----------------------------------|
| 1 | 1 | 1 |
| 2 | 10 | 2 |
| 3 | 11 | 2 |
| 4 | 100 | 3 |
| 7 | 111 | 3 |
| 8 | 1000 | 4 |
| 15 | 1111 | 4 |
| 16 | 10000 | 5 |
| 128 | 10000000 | 8 |
| 341 | 101010101 | 9 |
4294967295 => 11111111111111111111111111111111 => 32
So f(16)
would print or return 5
This is code-golf. Shortest code in bytes wins
floor(log2(num))+1
\$\endgroup\$num
is a power of two. \$\endgroup\$