简体   繁体   中英

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.

I know this is possible with shifting and counting but that uses O(N) operations. Is there some way to get O(1) for any n where x = 2^n ?

I want to find n here knowing x in one operation or O(1) .

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.

[ FYI: big-O notation refers to asymptotic complexity (ie as N -> infinity); it doesn't make much sense if N has a finite limit (32 or 64 in this case). ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM