Bit Manipulation

Overview

Bit manipulation verbs operate on the individual bits of 32-bit signed integers.

AND, OR, XOR, and NOT operate independently on each bit of their operands. Shift operations move bits left or right and discard bits that leave the 32-bit value. Rotation operations instead wrap those bits around to the opposite end.

Memory is untyped; operands are interpreted as integers by bit manipulation instructions.

Unless otherwise specified, bit manipulation instructions modify only their destination.

Bitwise Operations

NOT — Bitwise NOT

NOT <dst> <val>

Computes the bitwise complement of a value and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to complement.

Modified locations

Location Description
dst Bitwise result.

Examples

-- Invert every bit.
NOT $000 $001
-- > $000 = ~$001

AND — Bitwise AND

AND <dst> <lhs> <rhs>

Computes the bitwise AND of two values and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Bitwise result.

Examples

-- Apply a bit mask.
AND $000 $001 255
-- > $000 contains the lowest 8 bits of $001

OR — Bitwise OR

OR <dst> <lhs> <rhs>

Computes the bitwise OR of two values and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Bitwise result.

Examples

-- Combine two bit fields.
OR $000 $001 $002
-- > $000 contains all bits set in either value

XOR — Bitwise XOR

XOR <dst> <lhs> <rhs>

Computes the bitwise exclusive OR of two values and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Bitwise result.

Examples

-- Find changed bits.
XOR $000 $001 $002
-- > $000 contains bits that differ between the values

Shifts

Shift operations use the supplied bit count directly.

Positive counts shift in the indicated direction. Negative counts shift in the opposite direction.

Bits shifted beyond the boundary of the 32-bit value are discarded. Vacated bits are filled with zeroes, except for SHR, which extends the sign bit.

SHR performs an arithmetic right shift and extends the sign bit, while LSHR performs a logical right shift and introduces zeroes.

If the shift count is 32 or greater, the entire value is shifted out. The result is therefore 0, except for a negative value shifted right with SHR, which produces -1.

SHL — Shift Left

SHL <dst> <val> <bits>

Shifts a value left by the specified number of bits and stores the result in the destination.

Zeroes are shifted into the low end of the value.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to shift.
3 bits Integer value — Number of bits to shift.

Modified locations

Location Description
dst Shifted value.

Examples

-- Multiply by eight.
SHL $000 $001 3
-- > $000 = $001 << 3

-- A negative count shifts in the opposite direction.
SHL $000 $001 -3
-- > equivalent to SHR $000 $001 3

SHR — Arithmetic Shift Right

SHR <dst> <val> <bits>

Shifts a value right by the specified number of bits and stores the result in the destination.

The sign bit is extended into the high end of the value.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to shift.
3 bits Integer value — Number of bits to shift.

Modified locations

Location Description
dst Shifted value.

Examples

-- Divide a positive value by eight.
SHR $000 $001 3
-- > $000 = $001 >> 3

-- A negative count shifts in the opposite direction.
SHR $000 $001 -3
-- > equivalent to SHL $000 $001 3

Notes

  • SHR performs an arithmetic right shift.
  • The sign bit is preserved.

LSHR — Logical Shift Right

LSHR <dst> <val> <bits>

Shifts a value right by the specified number of bits and stores the result in the destination.

Zeroes are shifted into the high end of the value rather than extending the sign bit.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to shift.
3 bits Integer value — Number of bits to shift.

Modified locations

Location Description
dst Shifted value.

Examples

-- Logical and arithmetic shifts differ for negative values.
MOVE $001 -1

SHR $000 $001 1
-- > $000 = -1

LSHR $000 $001 1
-- > $000 = 2147483647

-- Extract the high byte of a packed value.
LSHR $000 $001 24
AND $000 $000 255

Notes

  • LSHR fills vacated high bits with zeroes.
  • Unlike SHR, the sign bit is not preserved.
  • Negative shift counts shift in the opposite direction.

Rotations

Rotation operations preserve bits that cross the boundary of the 32-bit value by wrapping them around to the opposite end.

Rotation counts are normalized modulo 32. Negative counts rotate in the opposite direction.

ROL — Rotate Left

ROL <dst> <val> <bits>

Rotates a 32-bit value left by the specified number of bits and stores the result in the destination.

Bits leaving the high end are wrapped around to the low end.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to rotate.
3 bits Integer value — Number of bits to rotate.

Modified locations

Location Description
dst Rotated value.

Examples

-- Rotate left by one bit.
ROL $000 $001 1

-- Rotation counts wrap at 32 bits.
ROL $000 $001 33
-- > equivalent to ROL $000 $001 1

-- Negative counts rotate in the opposite direction.
ROL $000 $001 -1
-- > equivalent to ROR $000 $001 1

ROR — Rotate Right

ROR <dst> <val> <bits>

Rotates a 32-bit value right by the specified number of bits and stores the result in the destination.

Bits leaving the low end are wrapped around to the high end.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to rotate.
3 bits Integer value — Number of bits to rotate.

Modified locations

Location Description
dst Rotated value.

Examples

-- Rotate right by one bit.
ROR $000 $001 1

-- Rotation counts wrap at 32 bits.
ROR $000 $001 33
-- > equivalent to ROR $000 $001 1

-- Negative counts rotate in the opposite direction.
ROR $000 $001 -1
-- > equivalent to ROL $000 $001 1