Bitwise Operations

Q

Which bit wise operator is suitable for checking whether a particular bit is on or off?

✍: FYIcenter

A

The bitwise AND operator. Here is an example:

enum {
   KBit1 = 0x00000001
   KBit2 = 0x00000010,
   KBit3 = 0x00000100,
   ...
};
if ( some_int & KBit24 )
   printf("Bit number 24 is ON\n");
else
   printf("Bit number 24 is OFF\n");

2007-02-26, 6324👍, 0💬