Select Page

OR places a 1 in the bitfield if either bitfields passed to it have a 1 in that position. If neither has a 1, it places a 0 in that position.

Truth Table:

 OR 1 0 1 1 1 0 1 0

Usage:

```// A bitfield whose binary representation is: 00000000000000000000000000001010
integer a = 10;

// Another bitfield whose binary representation is: 00000000000000000000000000011001
integer b = 25;

// The next line translates to "store the result of OR'ing a and b together" in the variable a_OR_b.
// Its binary representation is 00000000000000000000000000011011, or 27.
integer a_OR_b = a | b;```

Here’s that operation written out longwise:

```a      00000000000000000000000000001010 // As decimal = 10
b      00000000000000000000000000011001 // As decimal = 25
--------------------------------------------------------------------
a | b  00000000000000000000000000011011 // As decimal = 27```

OR is useful in combining two bits, a good use of this would be executing some statements if the user has both the up arrow and down arrow pressed in the control event.

```control(key controller, integer levels, integer edges) {
// If user is pressing both the up arrow and down arrow:
if ((levels & (CONTROL_FWD | CONTROL_BACK)) == (CONTROL_FWD | CONTROL_BACK)) {
// The above statement translates to: "If levels has both the CONTROL_FWD and CONTROL_BACK bits set to 1"
}
}```

Written out in longhand, the conditional statement looks like this:
The ORing operation, to get a bitfield with both CONTROL_FWD and CONTROL_BACK set to 1.

```CONTROL_FWD                  00000000000000000000000000000001 // As decimal = 1
CONTROL_BACK                 00000000000000000000000000000010 // As decimal = 2
-----------------------------------------------------------------------------------------
(CONTROL_FWD | CONTROL_BACK) 00000000000000000000000000000011 // As decimal = 3```

The ANDing operation, to see if the both CONTROL_FWD and CONTROL_BACK are set to 1 in levels.
In this case, levels is a bitfield in which CONTROL_FWD, CONTROL_BACK and CONTROL_UP are set to 1.

```(CONTROL_FWD | CONTROL_BACK)          00000000000000000000000000000011 // As decimal = 3
levels                                00000000000000000000000000010011 // As decimal = 19
-----------------------------------------------------------------------------------------
levels & (CONTROL_FWD | CONTROL_BACK) 00000000000000000000000000000011 // As decimal = 3

```

Credit to: Lslwiki.net (not working) with changes made for brevity and clarity.