Overview
The Romasm binary operations library (stdlib/binary.romasm) provides bitwise operations: AND, OR, XOR, and NOT. These operations work on the binary representation of integers.
Note: These are different from the Basic Romasm binary operations (which work on Roman symbol states). These operate on standard integer bit patterns.
Available Functions
bitwise_and
Performs bitwise AND operation: result = value1 & value2
Each bit in the result is 1 only if both corresponding bits in the inputs are 1.
| Input | Output |
|---|---|
| R0 = value1 R1 = value2 |
R0 = value1 & value2 |
; Calculate 5 & 3 = 1
; 5 = 101₂, 3 = 011₂
; AND = 001₂ = 1
LOAD R0, 5
LOAD R1, 3
CALL bitwise_and
PRINT R0 ; Outputs: 1
bitwise_or
Performs bitwise OR operation: result = value1 | value2
Each bit in the result is 1 if either corresponding bit in the inputs is 1.
| Input | Output |
|---|---|
| R0 = value1 R1 = value2 |
R0 = value1 | value2 |
; Calculate 5 | 3 = 7
; 5 = 101₂, 3 = 011₂
; OR = 111₂ = 7
LOAD R0, 5
LOAD R1, 3
CALL bitwise_or
PRINT R0 ; Outputs: 7
bitwise_xor
Performs bitwise XOR (exclusive OR) operation: result = value1 ^ value2
Each bit in the result is 1 if the corresponding bits in the inputs are different.
| Input | Output |
|---|---|
| R0 = value1 R1 = value2 |
R0 = value1 ^ value2 |
; Calculate 5 ^ 3 = 6
; 5 = 101₂, 3 = 011₂
; XOR = 110₂ = 6
LOAD R0, 5
LOAD R1, 3
CALL bitwise_xor
PRINT R0 ; Outputs: 6
bitwise_not
Performs bitwise NOT operation (complement): result = ~value
Flips all bits: 0 becomes 1, 1 becomes 0.
| Input | Output |
|---|---|
| R0 = value | R0 = ~value |
; Calculate ~5
; 5 = 101₂ (in 16-bit: 0000000000000101)
; NOT = 1111111111111010 = 65530 (16-bit)
LOAD R0, 5
CALL bitwise_not
PRINT R0 ; Outputs: 65530 (for 16-bit)
Implementation Details
These functions implement bitwise operations by:
- Processing bits one at a time (from least significant to most significant)
- Using MOD 2 to extract the lowest bit
- Using SHR (shift right) to move to the next bit
- Using SHL (shift left) to build the result
Note: The current implementation processes up to 32 bits, but can be extended for larger values.
Use Cases
- Flags: Pack multiple boolean flags into a single integer
- Masks: Extract specific bits from values
- Cryptography: XOR operations for simple encryption
- Low-level Programming: Direct bit manipulation
Related Documentation
- Instruction Set - SHL and SHR instructions
- Basic Math Functions - Other stdlib functions
- Linker System - How to use stdlib functions