Bit
Work with 32-bit values using arithmetic, bitwise operators, shifts, rotations, and hexadecimal conversion. The bit library is enabled by default.
Functions
bit.arshiftMove bits right while repeating the original high bit in the vacated positions. Use it for a signed bit pattern; the returned Luau number still represents an unsigned 32-bit value.
bit.baddAdd values with unsigned 32-bit wraparound. This is useful for calculations that must retain only the low 32 bits.
bit.bandKeep only bits set in every input value. Use a mask to extract a field or test whether an option bit is enabled.
bit.bdivDivide the first accepted value by each subsequent value using unsigned integer division. Fractional parts are discarded, making it useful for whole-unit calculations.
bit.bmulMultiply values and retain the low 32 bits of the result. Use it when a fixed-width format requires wrapping multiplication.
bit.bnotInvert every bit in a 32-bit value. Combine it with band to clear selected flags while preserving the others.
bit.borCombine the set bits from all input values. Use it to turn on several independent flags in one numeric value.
bit.bsubSubtract each subsequent value from the first, wrapping to 32 bits. Use it for unsigned differences in a fixed-width calculation.
bit.bswapReverse the order of the four bytes in a 32-bit value. Use it to convert between little-endian and big-endian representations.
bit.bxorSet a result bit when the inputs contain an odd number of set bits at that position. With two inputs, use it to toggle selected bits without changing the others.
bit.lshiftMove bits left and fill the low positions with zeroes, discarding overflow. Use it to place a number into a higher bit field.
bit.rolRotate bits left, moving bits that leave the high end back into the low end. Use it in fixed-width transformations where no bit should be discarded.
bit.rorRotate bits right, moving bits that leave the low end back into the high end. It reverses a left rotation by the same count.
bit.rshiftMove bits right and fill the high positions with zeroes. Combine it with a mask to read a field from a packed integer.
bit.tobitTruncate a finite number and wrap it into the unsigned 32-bit range. Use it to normalize a value before fixed-width calculations.
bit.tohexRepresent a 32-bit value as lowercase hexadecimal text. Use it to inspect masks, colors, or packed fields; the output has no 0x prefix or leading padding.