Record what knowledge learned today

Bitwise &, |, ^ and ~

#define function1 0x1

#define function2 0x2

#define function3 0x04

#define function4 0x08

int flag = 10001000; // flag may be used to represent the ON state of the eight functions, or for determining the protocol decoding

 

Open function:

flag |=function;

Examples of open Function 1

1000 1000 |

0000 0001=1000 1001;

Off function:

glag&=~function;

Examples closing function 4

 1001 1001&

~0000 1000=

1000 1001&

1111  0111=1000 0001

Check the bit values:

if(flags&function)

Examples: determining function is turned on 8

1000 0001&

1000 0000=

1000 0000

 

Guess you like

Origin www.cnblogs.com/iillegal/p/10994418.html
Recommended