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 - 0x00
  • true - 0x01

Bitwise Operations

ARM provides some instructions for bitwise operations:

  • AND - bitwise AND
  • ORR - bitwise OR
  • EOR - 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, R4

This can be optimised and written as such:

CMP R1, R2
ANDLT R3, R3, #1
ANDGE R3, R3, #0

Optimisation 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 = 0
  • A & 1 = A
  • A | 1 = 1
  • A | 0 = A Therefore, the example above can be further simplified in this way:
MOVGE R3, #0

Lazy 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 - Copy src to dst, then any bit set to 1 in Op2 is cleared from dst.
  • TST Op1, Op2 - Set flags from Op1 & Op2 and discard the result.