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.