C/C++ bit-op test_bit,set_bit,clear_bit like linux kernel
You might love the bit operation APIs in "bitops.h" @ Linux kernel driver and 'd like to use them as well in user space C/C++ applications and UEFI / DOS tools.
Here comes 32-bit bit operations examples like the following:
代碼:
/* Set __SHIFT bit */
void set_bit(uint8_t __SHIFT, uint32_t *var_addr)
{
*(var_addr) |= ( 0x1UL << __SHIFT);
}
/* Clear __SHIFT bit */
void clear_bit(uint8_t __SHIFT, uint32_t *var_addr)
{
*(var_addr) &= ~( 0x1UL << __SHIFT);
}
/* Test __SHIFT bit */
uint8_t test_bit(uint8_t __SHIFT, uint32_t *var_addr)
{
return (*(var_addr) >> __SHIFT) & 0x1UL;
}
void main(void)
{
uint32_t a = 0x0000feef;
set_bit( 8, &a);
printf("%s: 0x%x\n", __func__, a);
printf("%s: test bit 8 == %d\n", __func__, test_bit(8, &a));
clear_bit( 8, &a);
printf("%s: 0x%x\n", __func__, a);
printf("%s: test bit 8 == %d\n", __func__, test_bit(8, &a));
return;
}
=============================
$ ./a.out
main: 0xffef
main: test bit 8 == 1
main: 0xfeef
main: test bit 8 == 0
________________
美術插畫設計案子報價系統 v0.1 Beta
爪哇禾雀
|