X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=include%2Fbitops.h;h=d0225ffe77e54b8cf2ab174be52ebb665bd39b76;hb=b416f18295f7edcb5474396a6b26a59fd5ddb1f5;hp=e18f3b0710aea6df465992aaa6aaae748de46594;hpb=0f870f29388a36d32ec9ffaabe9ffe03edc038c0;p=smdp.git diff --git a/include/bitops.h b/include/bitops.h index e18f3b0..d0225ff 100644 --- a/include/bitops.h +++ b/include/bitops.h @@ -1,6 +1,23 @@ #if !defined( BITOPS_H ) #define BITOPS_H +/* + * Macros to do bit operations on integer variables. + * + * macro: SET_BIT to set bit at pos of var to 1 + * macro: CLEAR_BIT to set bit at pos of var to 0 + * macro: TOGGLE_BIT to toggle bit at pos of var from 1 to 0 and vice versa + * macro: CHECK_BIT returns > 0 if bit at pos of var is set to 1 + * + * Example: + * int i = 0; + * i = SET_BIT(i, 2); + * if(CHECK_BIT(i, 2)) + * printf("bit 2 is set\n"); + * printf("value of i is %d\n", i); + * + */ + #define SET_BIT(var, pos) ((var) |= (1<<(pos))) #define CLEAR_BIT(var, pos) ((var) &= (~(1<<(pos)))) #define TOGGLE_BIT(var, pos) ((var) ^= (1<<(pos)))