libbitwise wraps low-level integer bit operations and pointer/memory primitives into reusable helpers.
This library is useful when you need:
- predictable bit mask operations
- explicit pointer arithmetic
- controlled memory reads/writes over byte buffers
Import path: bitwise
bitwise := import('bitwise')
{
and: and
or: or
xor: xor
not: not
shl: shl
shr: shr
hasAll?: hasAll?
hasAny?: hasAny?
set: set
clear: clear
toggle: toggle
update: update
ptr: ptr
null: null
null?: null?
addrOf: addrOf
add: add
sub: sub
diff: diff
read: read
write: write
} := import('bitwise')
Bitwise AND.
Bitwise OR.
Bitwise XOR.
Bitwise NOT (implemented as n ^ -1).
Shift left by by bits.
Shift right by by bits.
{
and: and
or: or
xor: xor
not: not
shl: shl
shr: shr
} := import('bitwise')
println(and(13, 11)) // 9
println(or(13, 11)) // 15
println(xor(13, 11)) // 6
println(not(10)) // -11
println(shl(3, 4)) // 48
println(shr(48, 4)) // 3
Returns true when all mask bits are set in value.
Returns true when any mask bit is set in value.
Sets mask bits in value.
Clears mask bits in value.
Flips mask bits in value.
Calls set when enabled is true, else clear.
{
hasAll?: hasAll?
hasAny?: hasAny?
set: set
clear: clear
toggle: toggle
update: update
} := import('bitwise')
value := 10 // 1010
mask := 6 // 0110
println(hasAll?(value, mask)) // false
println(hasAny?(value, mask)) // true
println(set(value, mask)) // 14
println(clear(value, mask)) // 8
println(toggle(value, mask)) // 12
println(update(value, mask, true)) // 14
println(update(value, mask, false)) // 8
Creates/checks the null pointer.
Returns a pointer to the first byte in a byte string.
Pointer arithmetic in bytes.
Byte distance between pointers.
Reads bytes from memory and returns a byte string.
Writes bytes to memory. Accepts either byte string or list of bytes.
{
bits: bits
string: string
} := import('std')
{
addrOf: addrOf
add: add
diff: diff
read: read
write: write
} := import('bitwise')
buf := bits([65, 66, 67, 68]) // ABCD
base := addrOf(buf)
println(diff(add(base, 3), base)) // 3
println(string(read(base, 4))) // ABCD
write(add(base, 1), [90, 89])
println(string(read(base, 4))) // AZYD
shlandshrare provided as reliable wrappers when direct shift operators are inconvenient.- Pointer and memory APIs are unsafe by design: invalid addresses can crash the process.
- Keep source byte buffers alive while operating on their addresses.