Logo

Programming-Idioms

Write two functions log2d and log2u, which calculate the binary logarithm of their argument n rounded down and up, respectively. n is assumed to be positive. Print the result of these functions for numbers from 1 to 12.
New implementation

Type ahead, or select one

Explain stuff

To emphasize a name: _x → x

Please be fair if you are using someone's work

You agree to publish under the CC-BY-SA License

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
def log2d(n) =  n.bit_length - 1

def log2u(n) = 2 ** log2d(n) == n ? log2d(n) : log2d(n) + 1

(1..12).each{|n| puts "#{n}  #{log2d(n)}  #{log2u(n)}" }