3
Problem:
In your choice of language, write the shortest function that returns the floor of the base-2 logarithm of an unsigned 64-bit integer, or –1 if passed a 0. (Note: This means the return type must be capable of expressing a negative value.)
Test cases:
Your function must work correctly for all inputs, but here are a few which help illustrate the idea:
INPUT ⟶ OUTPUT
0 ⟶ -1
1 ⟶ 0
2 ⟶ 1
3 ⟶ 1
4 ⟶ 2
7 ⟶ 2
8 ⟶ 3
16 ⟶ 4
65535 ⟶ 15
65536 ⟶ 16
18446744073709551615 ⟶ 63
Rules:
- You can name your function anything you like.
- Character count is what matters most in this challenge.
- You will probably want to implement the function using purely integer and/or boolean artithmetic. However, if you really want to use floating-point calculations, then that is fine so long as you call no library functions. So, simply saying
return n?(int)log2l(n):-1;in C is off limits even though it would produce the correct result. If you're using floating-point arithmetic, you may use*,/,+,-, and exponentiation (e.g.,**or^if it's a built-in operator in your language of choice). This restriction is to prevent "cheating" by callinglog()or a variant. - If you're using floating-point operations (see #3), you aren't required that the return type be integer; only that that the return value is an integer, e.g., floor(log₂(n)).
- If you're using C/C++, you may assume the existence of an unsigned 64-bit integer type, e.g.,
uint64_tas defined instdint.h. Otherwise, just make sure your integer type is capable of holding any 64-bit unsigned integer. - If your langauge does not support 64-bit integers (for example, Brainfuck apparently only has 8-bit integer support), then do your best with that and state the limitation in your answer title. That said, if you can figure out how to encode a 64-bit integer and correctly obtain the base-2 logarithm of it using 8-bit primitive arithmetic, then more power to you!
- Have fun and get creative!
3Why the restriction to C? Language-specific challenges are generally frowned upon. Also, what's the meaning of the bonus? (And also I don't think there is any need to show two ungolfed solutions right away.) – Martin Ender – 2014-07-26T19:28:22.100
@MartinBüttner — Oh, ok, I didn't realize that. I'm new here (not to SX but to CG.SX). Thanks for pointing that out. I'll remove the restriction and delete the second example, and I'll eliminate the language-specific requirement. – Todd Lehman – 2014-07-26T19:30:32.880
@MartinBüttner — Actually, went ahead and deleted both examples. – Todd Lehman – 2014-07-26T19:33:02.090
6No floating point? There goes my best idea (inspired by the famous fast inverse square root.) Assign the number to float, cast it bitwise to an integer, and extract the exponent from it by rightshifting by a constant. – Level River St – 2014-07-26T19:36:04.437
@steveverrill — OK, I'll edit the question to allow floating-point so long as no external library functions are used. Looking forward to hearing your idea! – Todd Lehman – 2014-07-26T19:37:44.417
1
As you changed the rules for me I went ahead and posted :-) All questions on PPCG should have an objective winning criterion. My answer is not a winner under pure code golf. If it is your intention to reward creative answers, you should do so in an objective way. See this question for example: http://codegolf.stackexchange.com/q/23581/15599. Otherwise, you can delete your rule 3 and make it a pure code golf. I won't mind if you do that.
– Level River St – 2014-07-26T21:09:06.870@steveverrill — I'll delete rule 3 and make it a pure code golf. That doesn't preclude someone from posting a perverse solution for fun. :) – Todd Lehman – 2014-07-26T21:24:25.783
If only I knew enough about x86 machine code to submit a 2-instruction LZCNT and subtract from 63... – user2357112 supports Monica – 2014-07-27T12:44:41.693