Consider a variable unsigned int a;
in C.
Now say I want to set any i'th bit in this variable to '1'.
Note that the variable has some value. So a=(1<<i)
will not work.
a=a+(1<<i)
will work,but I am looking for the fastest way. Anything??
Bitwise or it. e.g. a |= (1<<i)
Some useful bit manipulation macros
#define BIT_MASK(bit) (1 << (bit))
#define SET_BIT(value,bit) ((value) |= BIT_MASK(bit))
#define CLEAR_BIT(value,bit) ((value) &= ~BIT_MASK(bit))
#define TEST_BIT(value,bit) (((value) & BIT_MASK(bit)) ? 1 : 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With