Garry's Mod Wiki

bit

The bitwise library contains useful functions for bitwise operations. Make sure you're familiar with Bitwise Operators

Methods

number bit.arshift( number value, number shiftCount )
Returns the arithmetically shifted value.
number bit.band( number value, ... )
Performs the bitwise and for all values specified.
number bit.bnot( number value )
Returns the bitwise not of the value.
number bit.bor( number value1, vararg ... )
Returns the bitwise OR of all values specified.
number bit.bswap( number value )
Swaps the byte order.
number bit.bxor( number value, number otherValues = nil )
Returns the bitwise xor of all values specified.
number bit.lshift( number value, number shiftCount )
Returns the left shifted value. The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.
number bit.rol( number value, number shiftCount )
Returns the left rotated value.
number bit.ror( number value, number shiftCount )
Returns the right rotated value.
number bit.rshift( number value, number shiftCount )
Returns the right shifted value. The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.
number bit.tobit( number value )
Normalizes the specified value and clamps it in the range of a signed 32bit integer.
string bit.tohex( number value, number digits = 8 )
Returns the hexadecimal representation of the number with the specified digits.

Example

Displays some bitwise operator functionality.

local mybits = 0 mybits = bit.bor(mybits, 2) -- Add 2 4 and 8 to mybits mybits = bit.bor(mybits, 4) mybits = bit.bor(mybits, 8) print(bit.band(mybits, 4) == 4) -- Does mybits have 4? mybits = bit.band(mybits, bit.bnot(4)) -- Remove 4 from mybits print(bit.band(mybits, 4) == 4) -- Does mybits have 4? print(bit.band(mybits, 2) == 2) -- Does mybits have 2?
Output:
true false true