这两个宏和那个函数是我从linux内核中截取出来的,在GCC编译器下(Dev C++)编译器下编译通过。
static inline int test_bit(int nr , const volatile void *addr) //测试位,测试addr的第nr位为0还是为1 { return 1 & (((const volatile unsigned int *)addr)[nr >> 5 ] >> (nr & 31)) ; } //设置位,把addr的第nr位置1 #define set_bit(nr,addr) ({\ register int res;\ __asm__ __volatile__("btsl %2,%3\n\t setb %%al"\ :"=a"(res) :"0" (0),"r"(nr),"m"(*(addr)));\ res;}) //清位,把addr的第nr位清0 #define clear_bit(nr,addr) ({\ register int res;\ __asm__ __volatile__("btrl %2,%3\n\t setnb %%al"\ :"=a"(res):"0"(0),"r"(nr),"m"(*(addr)));\ res;})
后面这几个是我模仿内核来写的两个以C实现的程序,比较容易理解:
static inline int __set_bit(int nr, const volatile void *addr) { unsigned long *add = (unsigned long *)addr ; return add[nr/32] |= (1UL << (nr % 32)); } static inline int __clr_bit(int nr , const volatile void *addr) { unsigned long *add = (unsigned long *)addr ; return add[nr/32] &= ~(1UL << (nr %32)); }
时间: 2024-10-30 03:13:36