I need to find the next power half the given number in Julia.
i.e. smallerpoweroftwo(15)must return 8, but smallerpoweroftwo(17)must return16
I still have it, but searching on a bit string seems a bit hacked to me. Maybe this is not ... Any ideas?
function smallerpoweroftwo(n::Int)
2^(length(bits(n)) - search(bits(n), '1'))
end
Thank!
Edit: I basically thought there was a more elegant way to do this, just using bitwise arithmetic. Or is there a bit length function somewhere, like in some other languages?
source
share