set_bit (int nr, volatile unsigned long *addr) : 设置addr内存地址所指向的值的第bit为1.
int flag=0x01;
unsigned long key=0x1110;
pr_info("key = 0x%x\n", (int)key);
set_bit(flag,&key);
如上,设置set_bit开始的内存地址的第1位(flag的值)为 1 ,注意是从0 开始计数的 。
也就是设置 0x1110的bit位的第二位为1 。,所以结果就是 0x1112.
clear_bit (int nr, volatile unsigned long *addr) : 设置addr内存地址所指向的值的第bit为0.
int flag=0x01;
unsigned long key=0x1112
clear_bit(flag,&key);
0x1112 的二进制的最后两位是 10 ,clear第二位之后 就是 00 ,也就是 key 的值变为了 0x1110.
test_bit (int nr, volatile unsigned long *addr) 测试某一位是否为1 。
以下是测试代码:
#include <linux/bug.h> /* For BUG_ON. */
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h>
#define TRUE(condition) BUG_ON(!(condition))
static int __init foo_init(void)
{
int flag=0x01;
unsigned long key=0x1110;
pr_info("key = 0x%x\n", (int)key);
set_bit(flag,&key);
pr_info("key = 0x%x\n", (int)key);
TRUE(key==0x1112);
TRUE(test_bit(flag,&key));
clear_bit(flag,&key);
pr_info("key = 0x%x\n", (int)key);
TRUE(key==0x1110);
flag=0x04;
set_bit(flag,&key);
pr_info("key = 0x%x\n", (int)key);
TRUE(key==(0x1110));
TRUE(test_bit(flag,&key));
flag=0x03;
set_bit(flag,&key);
pr_info("key = 0x%x\n", (int)key);
TRUE(key==(0x1118));
clear_bit(flag,&key);
TRUE(!test_bit(flag,&key));
TRUE(key==(0x1110));
flag=0x04;
clear_bit(flag,&key);
TRUE(key==(0x1100));
TRUE(!test_bit(flag,&key));
TRUE(key==(0x1100));
flag = 0x08;
change_bit(flag,&key);
TRUE(key==(0x1000));
pr_info("key = 0x%x\n", (int)key);
return 0;
}
static void __exit foo_cleanup(void)
{
}
module_init(foo_init);
module_exit(foo_cleanup);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");