不明白nextpow的工作原理
最近,我一直在研究nextPow(a :: Real,x :: real)
函数在内部工作。该代码在Julia项目的base/intfuncs.jl
中。对于情况a == 2
有一个优化,因为它很常见。
a == 2 && isa(x, Integer) && return _nextpow2(x)
但是,我不明白这种情况是如何工作的,我知道这种对位的操纵必须是找到下一个a^n
的常见的东西,但是现在我不明白。
_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)))
特别是我不明白的是函数_nextPow2(x :: unsigned)
。为什么这有助于获得a^n
大于x
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于无符号的int,无论其
bitstring()
是
放在最后一个领先0并用0000000001xxxxxxx
,2的下一个功率只是将1
0000000
替换整个1xxxxxx
for an unsigned Int, whatever its
bitstring()
is, say0000000001xxxxxx
, the next power of 2 is just put a1
at the last leading 0 and replace the entire1xxxxxx
with0000000