Binary Representation of Boolean Values
As there are 2 possible boolean values, 1 bit would be sufficient to store each value. This would allow 8 separate boolean values to be contained in a single byte. However, the overhead involved with this method, including extracting each bit from a byte, is generally considered a greater issue than the extra memory otherwise required. As such, a boolean value is represented with 1 byte:
false-0x00true-0x01
Bitwise Operations
ARM provides some instructions for bitwise operations:
AND- bitwise ANDORR- bitwise OREOR- bitwise XOR
Optimisation
Optimisation using Conditional Execution
Conditional execution of bitwise operations can be used to optimise the evaluation of complex conditions.
For example, a naïve implementation of the condition x & (y < z) may be:
CMP R1, R2
MOVLT R4, #1
MOVGE, R4, #0
AND R3, R3, R4This can be optimised and written as such:
CMP R1, R2
ANDLT R3, R3, #1
ANDGE R3, R3, #0Optimisation using Logical Equivalence
Replacing conditions with logical equivalents may allow evaluation with fewer instructions. In some cases, an obvious simplification may be possible. For example:
A & 0 = 0A & 1 = AA | 1 = 1A | 0 = ATherefore, the example above can be further simplified in this way:
MOVGE R3, #0Lazy Evaluation of Conditions
Using the facts stated above, it may be possible to determine the outcome of a condition without evaluating each component. For example, the condition A | B can be determined to be 1 without evaluating the value of B if A evaluates to 1.
More Bitwise Instructions
Some other useful bitwise instructions include:
BIC dst, src, Op2- Copysrctodst, then any bit set to1inOp2is cleared fromdst.TST Op1, Op2- Set flags fromOp1 & Op2and discard the result.