c++ - 如何实现 MS 特定的 _BitScanReverse() 函数?

标签 c++ visual-c++ gcc bit

来自 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/

相关文章:

c++ - 谁能解释一下当前C++0x标准草案的这一段?

c++ - 混合类和函数模板

C++ 错误 : braces around scalar initializer for type 'bool'

c++ - Visual Studio malloc 限制

c++ - 在简单的蛇游戏中使用未声明的标识符错误

visual-c++ - 如何在 Visual Studio 2008 C++ 中链接 .lib

c++ - 请给我一个 boost-multi-index stub - 带有 std::cout

windows - 堆栈缓冲区溢出(Windows、C++): how can I detect the culprit?

c++ - 抑制单行的弃用警告

C++ : declaring in if/else : var not declared in this scope