从 unsigned int (C++) 读取最左边位的最快方法?
从 unsigned int 读取最左边位的最快方法是什么?
What is the fastest way to read the Left-Most bit from unsigned int ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
从 unsigned int 读取最左边位的最快方法是什么?
What is the fastest way to read the Left-Most bit from unsigned int ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
sizeof、乘法和减法将由任何合理的编译器在编译时计算,因此这应该成为单个右移指令,这与您所能获得的速度差不多。
The
sizeof
, multiplication, and subtraction will be computed at compile-time by any reasonable compiler, so this should become a single right-shift instruction, which is about as fast as you will get.如果 AND 比移位更快,则可能比运行时移位更快:
Might be faster than shifting at run-time, if AND is faster than shifting:
在使用普通人会使用的任何编译器的 32 位系统上,没有 C++ 极客似乎无法避免兴奋的奇怪、深奥的边缘情况,地球,大约 2010 年,星星未对齐,正常的一天:
On a 32-bit system using any compiler that a normal human would use, without odd, esoteric edge cases that C++ geeks can't seem to avoid getting excited about, planet Earth, circa 2010, stars unaligned, a normal day:
出于示例目的,假设 8 位 int 将其分解。
然后将该值与您要测试的值相与(转换为 unsigned int )
breaking it down assuming 8 bits int for example purposes.
then AND this value with what you want to test ( cast as unsigned int )
您当然也可以尝试这个:
并使用以下事实:对于有符号整数,最左边的位设置为负数。
这也应该相当快,因为强制转换是编译时的事情,并且实际上并不需要执行 - 它只是告诉编译器使用签名的操作码而不是未签名的操作码。所以我相信需要执行一条指令(
CMP
?)...You could of course also try this:
And use the fact, that for signed integers, the left most bit is set for negative numbers.
This should also be pretty fast, since the cast is a compile-time thing, and does not really have to be executed - it just tells the compiler to use the signed opcodes as opposed to the unsigned ones. So I believe a single instruction (
CMP
?) needs to be executed...这个怎么样?
How about this?
这取决于 int 的大小、字节顺序,以及您是否想要基于内存中方向的最左边位或最高有效位。
猜测您想要最高有效位,在小端、32 位机器(最常见的类型)上,该位将位于内存中
int
位置的第四个字节中。:现在是寄存器是一个布尔值,表示 MSB 位是否存在。
可能会向右移位,这可能会生成更少的目标代码:
Update0
也许有些人不知道我上面的意思。在我给出的示例架构中,您也可以这样做:
Well that depends on the size of int, endianness, and whether you want the left most bit based on orientation within memory or the most significant bit.
Guessing that you want the most significant bit, which on a little endian, 32 bit machine (the most common kind), would be in the fourth byte from the
int
s location in memory.:Now the register is a boolean for the presence of the MSB bit.
It might be possible to bit shift to the right, this may generate less object code:
Update0
Maybe some aren't aware of what I mean above. On the example architecture I gave you could also do this: