c - 用于计数位集的内核宏

标签 c linux-kernel bit-manipulation linux-device-driver device-driver

是否有标准的 Linux 内核位操作宏,它返回 unsigned long 中设置的位数?

最佳答案

您可以使用:

hweight_long

include/linux/bitops.h中的函数

否则 Linux 内核使用 gcc带有 GNU 扩展和 gcc还提供了这些内置函数:

Built-in Function: int __builtin_popcount (unsigned int x) Returns the number of 1-bits in x.

Built-in Function: int __builtin_popcountl (unsigned long) Similar to __builtin_popcount, except the argument type is unsigned long.

http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

关于c - 用于计数位集的内核宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14366069/

相关文章:

c - 使用scanf()读取 "00"

c - 函数陷入循环

c - 修改内核模块中的控制寄存器

c - 理解 Linux 内核中的 hlist_bl_for_each_entry_rcu 宏

c - 用C从二进制文件中一点一点地读取

c++ - 如何优化这个将输入位转换为单词的简单函数?

c# - 从字节数组中读取位范围

c - 在C程序中获取命令行结果

c - 为什么在 C 的 header 中使用 extern 关键字?

将 raspbian wheezy usb 驱动程序 ch341.c 编译为 ch341.ko