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.
Unless otherwise specified, bit manipulation instructions do not modify any registers other than their destination.
Bitwise operations
NOT — Bitwise NOT
NOT <dst> <val>
Computes the bitwise complement of a value and stores the result in the destination register.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to complement. |
Modified registers
| Register | Description |
|---|---|
dst |
Bitwise result. |
Examples
-- Invert every bit.
NOT r0 r1
-- > r0 = ~r1
AND — Bitwise AND
AND <dst> <lhs> <rhs>
Computes the bitwise AND of two values and stores the result in the destination register.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | lhs |
Integer value | — | Left-hand operand. |
| 3 | rhs |
Integer value | — | Right-hand operand. |
Modified registers
| Register | Description |
|---|---|
dst |
Bitwise result. |
Examples
-- Apply a bit mask.
AND r0 r1 255
-- > r0 contains the lowest 8 bits of r1
OR — Bitwise OR
OR <dst> <lhs> <rhs>
Computes the bitwise OR of two values and stores the result in the destination register.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | lhs |
Integer value | — | Left-hand operand. |
| 3 | rhs |
Integer value | — | Right-hand operand. |
Modified registers
| Register | Description |
|---|---|
dst |
Bitwise result. |
Examples
-- Combine two bit fields.
OR r0 r1 r2
-- > r0 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 register.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | lhs |
Integer value | — | Left-hand operand. |
| 3 | rhs |
Integer value | — | Right-hand operand. |
Modified registers
| Register | Description |
|---|---|
dst |
Bitwise result. |
Examples
-- Find changed bits.
XOR r0 r1 r2
-- > r0 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.
SHR performs an arithmetic right shift and extends the sign bit, while LSHR performs a logical right shift and introduces zeroes.
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.
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 register.
Zeroes are shifted into the low end of the value.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to shift. |
| 3 | bits |
Integer value | — | Number of bits to shift. |
Modified registers
| Register | Description |
|---|---|
dst |
Shifted value. |
Examples
-- Multiply by eight.
SHL r0 r1 3
-- > r0 = r1 << 3
-- A negative count shifts in the opposite direction.
SHL r0 r1 -3
-- > equivalent to SHR r0 r1 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 register.
The sign bit is extended into the high end of the value.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to shift. |
| 3 | bits |
Integer value | — | Number of bits to shift. |
Modified registers
| Register | Description |
|---|---|
dst |
Shifted value. |
Examples
-- Divide a positive value by eight.
SHR r0 r1 3
-- > r0 = r1 >> 3
-- A negative count shifts in the opposite direction.
SHR r0 r1 -3
-- > equivalent to SHL r0 r1 3
Notes
SHRperforms 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 register.
Zeroes are shifted into the high end of the value rather than extending the sign bit.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to shift. |
| 3 | bits |
Integer value | — | Number of bits to shift. |
Modified registers
| Register | Description |
|---|---|
dst |
Shifted value. |
Examples
-- Logical and arithmetic shifts differ for negative values.
MOVE r1 -1
SHR r0 r1 1
-- > r0 = -1
LSHR r0 r1 1
-- > r0 = 2147483647
-- Extract the high byte of a packed value.
LSHR r0 r1 24
AND r0 r0 255
Notes
LSHRfills 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 register.
Bits leaving the high end are wrapped around to the low end.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to rotate. |
| 3 | bits |
Integer value | — | Number of bits to rotate. |
Modified registers
| Register | Description |
|---|---|
dst |
Rotated value. |
Examples
-- Rotate left by one bit.
ROL r0 r1 1
-- Rotation counts wrap at 32 bits.
ROL r0 r1 33
-- > equivalent to ROL r0 r1 1
-- Negative counts rotate in the opposite direction.
ROL r0 r1 -1
-- > equivalent to ROR r0 r1 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 register.
Bits leaving the low end are wrapped around to the high end.
Operands
| Position | Name | Type | Range | Description |
|---|---|---|---|---|
| 1 | dst |
Integer register | — | Destination register. |
| 2 | val |
Integer value | — | Value to rotate. |
| 3 | bits |
Integer value | — | Number of bits to rotate. |
Modified registers
| Register | Description |
|---|---|
dst |
Rotated value. |
Examples
-- Rotate right by one bit.
ROR r0 r1 1
-- Rotation counts wrap at 32 bits.
ROR r0 r1 33
-- > equivalent to ROR r0 r1 1
-- Negative counts rotate in the opposite direction.
ROR r0 r1 -1
-- > equivalent to ROL r0 r1 1