Bitwise Operations
Learn how to perform bitwise operations in your programs.
Reference
Name | Operation | Description |
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
2 3 << # The stack now contains: 16
# Shifting bits right
16 2 >> # The stack now contains: 4