Recently I have been looking at how the nextpow(a::Real, x::Real) function works inside. The code is in base/intfuncs.jl of the Julia project. For the case a == 2 there is an optimization as it is very common as case.
a == 2 && isa(x, Integer) && return _nextpow2(x)
However, I don't understand how this case works, I understand that this manipulation of bits must be something common to find the next a^n, but right now I don't understand it.
_nextpow2(x::Unsigned) = oneunit(x)<<((sizeof(x)<<3)-leading_zeros(x-oneunit(x)))
_nextpow2(x::Integer) = reinterpret(typeof(x),x < 0 ? -_nextpow2(unsigned(-x)) : _nextpow2(unsigned(x)))
In particular what I don't understand is the function _nextpow2(x::Unsigned). Why does this help to get a^n larger than x?