来自 MSDN 从最高有效位 (MSB) 到最低有效位 (LSB) 的掩码数据中搜索设置位 (1)。
无符号字符 _BitScanReverse ( 无符号长*索引, 无符号长掩码 );
参数 [出]索引 加载找到的第一个设置位 (1) 的位位置。
[in] 面具 要搜索的 32 位或 64 位值。
返回值 如果掩码为零则为 0;否则非零。
备注 如果找到设置位,则在第一个参数中返回找到的第一个设置位的位位置。如果没有找到设置位,则返回 0;否则,返回 1。
请告诉我如何在 OS X 上实现安全快速的 _BitScanReverse() 函数?我必须使用汇编还是有更简单的方法?
最佳答案
GCC 有一些类似的内置函数:
— Built-in Function: int __builtin_clz (unsigned int x)
Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
— Built-in Function: int __builtin_ctz (unsigned int x)
Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined
如果你有零的个数,你应该能够找出第一个 1 在哪里。 :-)
关于c++ - 如何实现 MS 特定的 _BitScanReverse() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173845/