function bitlen(x) return x == 0 and 1 or math.ceil(math.log(x+1)/math.log(2)) end
Probably, unless you try it with certain powers of two, like 2^49 or above. I didn't try such approach because it has precision problems. I could have tested which was the first number it failed with, but I just stayed away from it. I could also have made a loop but since that function was in a hot loop, I wanted it fast, so I organized it as a hardcoded binary search. Also, short loops are not LuaJIT-friendly and might break traces.
it actually failed at 2^39-1 for me; that should have returned 39, but returned 40 instead... interestingly 2^40-1 worked again up until 2^47-1, which failed, then 2^48-1 worked again, up until 2^51-1 which failed.
Still, for up to 32 bits, the function should work reliably, although i didn't test it (i did up to 2^23, online interpreter times out above that), but i see that logarithms do tend to cause inaccuracies with this method, compared to just comparisons with constants; that should work up to the mantissa limit for whatever number type lua uses.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.