- Calculates the bitwise not of a number.
- num
-
- The number.
- return-value
-
- The bitwise-not of num.
The bnot()
function inverts all of the bits of a number.
Numbers in PICO-8 are stored using a 32-bit fixed point format, with 16 bits for the integer portion, 16 bits for the fractional portion, and a twos complement representation for negative and positive values. As a result, the bitwise-not of a number tends to be its negative. (bnot(0)
is almost 0, but not quite.)
In cases where you're using bnot()
you probably only care about specific bits and not the entire number. You can band()
the result to mask out the bits you don't care about.
Superseded by ~ operator[]
The unary ~
operator added in 0.2.0 performs the same function as bnot()
and is now the recommended way to invert bits, as it uses fewer tokens, costs fewer cycles at runtime, and runs on the real host CPU much more efficiently. Simply replace bnot(a)
with ~a
.
Examples[]
-- 0xb = 000...1011
-- not 0xb = 111...0100
-- 0xf = 000...1111
-- (not 0xb) and 0xf = 000...0100 (0x4)
print(band(bnot(0xb), 0xf)) -- 4
print(bnot(0xb)) -- -11 (0 - 0xb)
print(~0xb) -- preferred method