Bitwise Operations

Learn how to perform bitwise operations in your programs.

Reference

NameOperationDescription
AND&( a b — a&b )Pops the last 2 numbers from the stack and pushes their bitwise AND or common logic gate if both values are boolean.
OR|( a b — a|b )Pops the last 2 numbers from the stack and pushes their bitwise OR or common logic gate if both values are boolean.
NOT~( a — ~a )Pops the top number from the stack and pushes its bitwise NOT or common logic gate if value is boolean.
Shift Left<<( a b — a<<b )Pops the last 2 numbers from the stack (value and shift amount) and pushes the result of shifting the value left.
Shift Right>>( a b — a>>b )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

Possible Errors