PICO-8 Wiki
Superseded / Deprecated
The feature described in this article has been superseded by a newer feature. This feature still works in PICO-8, but the new feature should be used instead. See the article's "Superseded by..." section for specific details.
bnot( num )
Calculates the bitwise not of a number.
The number.

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.


--               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

See also[]