Bitwise Operations

Learn how to perform bitwise operations in your programs.

Reference

NameOperationDescription
AND&Pops the last 2 numbers from the stack and pushes their bitwise AND.
OR|Pops the last 2 numbers from the stack and pushes their bitwise OR.
NOT~Pops the top number from the stack and pushes its bitwise NOT.
Shift Left<<Pops the last 2 numbers from the stack (value and shift amount) and pushes the result of shifting the value left.
Shift Right>>Pops the last 2 numbers from the stack (value and shift amount) and pushes the result of shifting the value right.

Usage

Use bitwise operations to manipulate binary data directly. Here are a few examples:

# Performing a bitwise AND
6 3 & # The stack now contains: 2

# Performing a bitwise OR
4 1 | # The stack now contains: 5

# Performing a bitwise NOT
5 ~ # The stack now contains: -6 (two's complement)

# Shifting bits left
# Remember about LIFO, the order is reversed, it is 3 << 2 and not 2 << 3
2 3 << # The stack now contains: 12

# Shifting bits right
# Remember about LIFO, the order is reversed, it is 16 >> 2 and not 2 >> 16
2 16 >> # The stack now contains: 4

Possible Errors