- Home /
Bitwise OR vs AND for combining rigidbody constraints
Why is the bitwise OR and not AND used to combine rigidbody constraints?
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-constraints.html
Answer by phodges · Nov 12, 2012 at 06:45 AM
This is fundamental to how these boolean operators work. Each constraint is a separate bit. Consider the following example, written in binary:
myConstraint1 = 01 myConstraint2 = 10
If I was to apply the bitwise AND operator to these constraints I would get nothing (01 & 10 = 0). If I was to apply the bitwise OR operator to them, I would collect both bits (01 | 10 = 11) and so this is equivalent to requiring that both constraints be respected. This merged mask is then checked by later code to see which constraints should be applied, using an AND operation to pick out specific items, i.e.
mergedConstraints & myConstraint1
If this test is non-zero then I know that the constraint should be applied.
Use bitwise OR (|) to turn a bit on:
mergedConstraints = myConstraint1 | myConstraint2;
In order to turn some bit off, AND it to the negated mask. If you want to clear myConstrant1, for instance, do this:
mergedConstraints &= ^myConstraint1;
Never use + or - to set/clear bits: if the bit is already in the desired state, other bits will be affected.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083728im_/https://answers.unity.com/themes/thub/images/avi.jpg)