[英]Binary logarithm in O(1) time (operating on registers x86 or SIMD) without shifting?
I wanted to see if there is a method for finding the binary log of a number. 我想看看是否有找到数字二进制日志的方法。 Say you have the number 4 then the power to which you raise two to get four is 2.
假设您有数字4,那么您将2乘以4的乘方就是2。
I know this is possible with shifting and counting but that uses O(N)
operations. 我知道这可以通过移位和计数实现,但是使用
O(N)
运算。 Is there some way to get O(1)
for any n
where x = 2^n
? 对于
x = 2^n
任何n
,是否有某种方法可以获取O(1)
?
I want to find n
here knowing x
in one operation or O(1)
. 我想在一个操作中知道
x
或O(1)
来找到n
。
As you've specified x86, it sounds like you want the BSR
(bit-scan reverse) opcode, which reports the position of the most-significant set bit. 正如您指定的x86一样,听起来像您想要
BSR
(反向位扫描)操作码,该操作码报告最高有效置位的位置。
[ FYI: big-O notation refers to asymptotic complexity (ie as N -> infinity); [ 仅供参考:big-O符号表示渐近复杂度(即N->无穷大); it doesn't make much sense if N has a finite limit (32 or 64 in this case).
如果N有一个有限的限制(在这种情况下为32或64)并没有多大意义。 ]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.