bit.test

Bitwise 'test'

Prototype

n = bit.test (a, b, ...)

Description

This takes one or more arguments. All are converted to signed 'long long' (64-bit integers).

The second and subsequent arguments are or'ed together. The result is then and'ed with the first argument. The function returns true if the result of the 'and' is the value being 'and'ed with.

Effectively this tests whether or not various bits are set in the first argument.

The result is a boolean (true or false) and thus can be directly used in "if" tests.

eg.

print (bit.test (0x42, 0x02)) --> true
print (bit.test (0x42, 0x40, 0x02)) --> true
print (bit.test (0x02, 0x03)) --> false (0x01 bit not set)
See also bit.band and bit.bor.

Note that setting a bit can be done with bit.bor.
Clearing a bit can be done with bit.clear.

Lua functions

Topics