What does "variable | variable" mean in C ++?

I looked at this ITE8712 watchdog demo code when I saw this:

void InitWD(char cSetWatchDogUnit, char cSetTriggerSignal) { OpenIoConfig(); //open super IO of configuration for Super I/O SelectIoDevice(0x07); //select device7 //set watch dog counter of unit WriteIoCR(0x72, cSetWatchDogUnit|cSetTriggerSignal); //CloseIoConfig(); //close super IO of configuration for Super I/O } 

and, interestingly, what is meant by this line:

 cSetWatchDogUnit|cSetTriggerSignal 

because the WriteIoCR function looks like this:

 void WriteIoCR(char cIndex, char cData) { //super IO of index port for Super I/O //select super IO of index register for Super I/O outportb(equIndexPort,cIndex); //super IO of data for Super I/O //write data to data register outportb(equDataPort,cData); } 

So cIndex should be 0x72, but what about cData? I really don't get "|" because I used it only for OR ("||") in the conditional expression.

+7
source share
5 answers

This is a bitwise or , unlike your usual boolean or . It basically sets the bits in the target variable if the corresponding bit was set in any of the source variables.

For example, the expression 43 | 17 43 | 17 can be calculated as:

 43 = 0x2b = binary 0010 1011 17 = 0x11 = binary 0001 0001 ==== ==== "or" them: 0011 1011 = 0x3b = 59 

See this answer for a closer look at the various bitwise operators.

It is usually used when you want to manipulate certain bits in a data type, for example, to control the watchdog timer in the embedded system (your specific use case).

You can use or (|) to turn bits on and and (&) to turn them off (with the inverse of the bitmask used to turn them on.

So, to enable bit b3 , use:

 val = val | 0x08; // 0000 1000 

To disable it, use:

 val = val & 0xf7; // 1111 0111 

To determine if b3 currently installed, use:

 if ((val & 0x08) != 0) { // it is set. } 

You will usually see that bitmasks have defined something like:

 #define B0 0x01 #define B1 0x02 #define B2 0x04 #define B3 0x08 #define B4 0x10 

or

 enum BitMask { B0 = 0x01, B1 = 0x02, B2 = 0x04, B3 = 0x08, B4 = 0x10 }; 

What does it mean:

 WriteIoCR (0x72, cSetWatchDogUnit|cSetTriggerSignal); 

Most likely, 0x72 will be the input / output port that you write, and cSetWatchDogUnit and cSetTriggerSignal will be the bits you combine to output the command (set the trigger signal and use the unit value for the watchdog timer). What this command means in practice can be inferred, but you are safer by referring to the documentation for the watchdog circuit itself.

And, in time, so that you don’t know what the watchdog is for, this is a simple circuit that, if you don’t kick it often enough (using another command), it will reset your system, possibly activating the reset output on any processor, which you use.

This is a way to automatically detect bad software behavior and return the device to a known initial state by subscribing to the theory that it is better to reboot than to continue to work badly.

+14
source

This is bitwise or . It is used here to combine flags.

+8
source

x | y x | y commonly used with plain old dates in C / C ++. This means bitwise OR .

eg.

 char x = 0x1, y = 0x2; x | y ==> 0x3 

[Note: operator | may be overloaded for class/struct to suit your needs.]

+1
source

| is bitwise or. It switches bits to (1 instead of 0) if one OR the other from the same bit in any of the integers.

|| is logical or. It returns true if one OR the other is true.

+1
source

OK, that’s why you use bitwise or, or you see that they are used in such a situation.

Often these variables are flags that are used to pack multiple pieces of data into one char

If cSetWatchDogUnit and cSetTriggerSignal

have non-overlapping bits (imagine cSetWatchDogUnit = 1 << 0 and cSetTriggerSignal = 1 << 1 , you can check later to see if they are set to size and, like this contrived example:

 if cData & cSetWatchDogUnit do something if cData & cSetTriggerSignal do something else 

All the time, both of these flags can be packed and passed in one char. This way you will not pass the bools array, you can add new constants cSetSomeOtherDamnfoolThing = 1 << 2 , and you can refer to flags as variables in your code.

0
source

All Articles