integerAdded by Luau 0.715.
LuauAvailable in luwu/integration; this feature is unstable and may change without a semver bump.
Needs the LuauIntegerLibrary FFlag.
experimentalDeprecated by Luau and Luwu.
Luau is removing integers and Luwu is replacing them with a new integer implementation.
deprecated
LuauIntegerLibrary FFlag.Deprecated
Luau is removing these integers. Luwu will replace them with signed and unsigned integer types that have arithmetic operators.
See Compatibility
The integer library creates and converts 64-bit integers and does their arithmetic, comparisons and bitwise operations.
An integer is a signed 64-bit whole number. A number holds whole numbers exactly only up to 2^53, so IDs, hashes and file offsets past that need an integer. Write one as a literal with an i suffix: 42i, 0xffi or 0b1010i.
Integers have no arithmetic or ordering operators. 1i + 2i and 1i < 2i are runtime errors and every operation goes through this library instead. == and ~= work. An integer never equals a number, and 1i and 1 are different table keys. typeof returns "integer" and tostring prints the value in decimal without the suffix.
Arithmetic wraps around on overflow like two's complement hardware does. Functions with a u prefix read their arguments as unsigned 64-bit integers.
const big = integer.add(integer.maxsigned, 1i)
print(big == integer.minsigned) --> true
const total = integer.mul(1_000_000i, 3_000_000i)
print(total) --> 3000000000000Summary
Creating and converting
function integer.create(x: number): integer?integer.createAdded by Luau 0.715.
Luau- Converts a number to an integer
function integer.fromstring(str: string, base: number?): integer?integer.fromstringAdded by Luau 0.715.
Luau- Parses a string as an integer in a given base
function integer.tonumber(x: integer): numberinteger.tonumberAdded by Luau 0.715.
Luau- Converts an integer to the nearest number
Limits
integer.maxsigned: integerinteger.maxsignedAdded by Luau 0.715.
Luau- Holds the largest integer (2^63 - 1)
integer.minsigned: integerinteger.minsignedAdded by Luau 0.715.
Luau- Holds the smallest integer (-2^63)
Arithmetic
function integer.neg(x: integer): integerinteger.negAdded by Luau 0.715.
Luau- Negates an integer
function integer.add(x: integer, y: integer): integerinteger.addAdded by Luau 0.715.
Luau- Adds two integers and wraps around on overflow
function integer.sub(x: integer, y: integer): integerinteger.subAdded by Luau 0.715.
Luau- Subtracts one integer from another and wraps around on overflow
function integer.mul(x: integer, y: integer): integerinteger.mulAdded by Luau 0.715.
Luau- Multiplies two integers and wraps around on overflow
function integer.div(x: integer, y: integer): integerinteger.divAdded by Luau 0.715.
Luau- Divides and rounds the quotient toward zero
function integer.idiv(x: integer, y: integer): integerinteger.idivAdded by Luau 0.715.
Luau- Divides and rounds the quotient down
function integer.rem(x: integer, y: integer): integerinteger.remAdded by Luau 0.715.
Luau- Returns the remainder of integer.div
function integer.mod(x: integer, y: integer): integerinteger.modAdded by Luau 0.715.
Luau- Returns the remainder of integer.idiv
function integer.udiv(x: integer, y: integer): integerinteger.udivAdded by Luau 0.715.
Luau- Divides two integers read as unsigned
function integer.urem(x: integer, y: integer): integerinteger.uremAdded by Luau 0.715.
Luau- Returns the remainder of integer.udiv
Comparison
function integer.min(first: integer, ...: integer): integerinteger.minAdded by Luau 0.715.
Luau- Returns the smallest of its arguments
function integer.max(first: integer, ...: integer): integerinteger.maxAdded by Luau 0.715.
Luau- Returns the largest of its arguments
function integer.clamp(x: integer, min: integer, max: integer): integerinteger.clampAdded by Luau 0.715.
Luau- Limits an integer to a range
function integer.lt(x: integer, y: integer): booleaninteger.ltAdded by Luau 0.715.
Luau- Checks whether x < y as signed integers
function integer.le(x: integer, y: integer): booleaninteger.leAdded by Luau 0.715.
Luau- Checks whether x <= y as signed integers
function integer.gt(x: integer, y: integer): booleaninteger.gtAdded by Luau 0.715.
Luau- Checks whether x > y as signed integers
function integer.ge(x: integer, y: integer): booleaninteger.geAdded by Luau 0.715.
Luau- Checks whether x >= y as signed integers
function integer.ult(x: integer, y: integer): booleaninteger.ultAdded by Luau 0.715.
Luau- Checks whether x < y as unsigned integers
function integer.ule(x: integer, y: integer): booleaninteger.uleAdded by Luau 0.715.
Luau- Checks whether x <= y as unsigned integers
function integer.ugt(x: integer, y: integer): booleaninteger.ugtAdded by Luau 0.715.
Luau- Checks whether x > y as unsigned integers
function integer.uge(x: integer, y: integer): booleaninteger.ugeAdded by Luau 0.715.
Luau- Checks whether x >= y as unsigned integers
Bitwise operations
function integer.band(...: integer): integerinteger.bandAdded by Luau 0.715.
Luau- Returns the bitwise and of its arguments
function integer.bor(...: integer): integerinteger.borAdded by Luau 0.715.
Luau- Returns the bitwise or of its arguments
function integer.bxor(...: integer): integerinteger.bxorAdded by Luau 0.715.
Luau- Returns the bitwise exclusive or of its arguments
function integer.bnot(x: integer): integerinteger.bnotAdded by Luau 0.715.
Luau- Flips every bit of an integer
function integer.btest(...: integer): booleaninteger.btestAdded by Luau 0.715.
Luau- Checks whether the bitwise and of its arguments isn't zero
Shifts and rotations
function integer.lshift(x: integer, shift: integer): integerinteger.lshiftAdded by Luau 0.715.
Luau- Shifts bits left and fills the vacated bits with zeros
function integer.rshift(x: integer, shift: integer): integerinteger.rshiftAdded by Luau 0.715.
Luau- Shifts bits right and fills the vacated bits with zeros
function integer.arshift(x: integer, shift: integer): integerinteger.arshiftAdded by Luau 0.715.
Luau- Shifts bits right and fills the vacated bits with the sign bit
function integer.lrotate(x: integer, shift: integer): integerinteger.lrotateAdded by Luau 0.715.
Luau- Rotates bits left
function integer.rrotate(x: integer, shift: integer): integerinteger.rrotateAdded by Luau 0.715.
Luau- Rotates bits right
Bit fields, counting and byte order
function integer.extract(x: integer, field: integer, width: integer?): integerinteger.extractAdded by Luau 0.715.
Luau- Returns a field of bits as an unsigned value
function integer.replace(x: integer, value: integer, field: integer, width: integer?): integerinteger.replaceAdded by Luau 0.715.
Luau- Replaces a field of bits with the low bits of a value
function integer.countlz(x: integer): integerinteger.countlzAdded by Luau 0.715.
Luau- Counts the zero bits above the highest set bit
function integer.countrz(x: integer): integerinteger.countrzAdded by Luau 0.715.
Luau- Counts the zero bits below the lowest set bit
function integer.bswap(x: integer): integerinteger.bswapAdded by Luau 0.715.
Luau- Reverses the order of an integer's 8 bytes
Creating and converting
integer.createAdded by Luau 0.715.
Luau
function integer.create(x: number): integer?Converts a number to an integer. It returns nil when x has a fractional part or lies outside the signed 64-bit range.
print(integer.create(7)) --> 7
print(integer.create(2.5)) --> nil
print(integer.create(2^63)) --> nilThe type checker declares the result as integer even though it can be nil.
integer.fromstringAdded by Luau 0.715.
Luau
function integer.fromstring(str: string, base: number?): integer?Parses str as an integer in base. The base defaults to 10 and must be from 2 to 36. It returns nil when the string isn't a valid integer. Surrounding whitespace is allowed.
In base 10 the string may start with a sign or with 0x for hexadecimal, and values beyond the signed range clamp to integer.minsigned or integer.maxsigned. Other bases parse the digits as an unsigned 64-bit value: integer.fromstring("ffffffffffffffff", 16) is -1i.
print(integer.fromstring("-42")) --> -42
print(integer.fromstring("ff", 16)) --> 255
print(integer.fromstring("12i")) --> nilThe type checker declares the result as integer even though it can be nil.
integer.tonumberAdded by Luau 0.715.
Luau
function integer.tonumber(x: integer): numberConverts an integer to the nearest number. Values beyond 2^53 lose precision: integer.tonumber(9007199254740993i) is 9007199254740992.
Limits
integer.maxsignedAdded by Luau 0.715.
Luau
integer.maxsigned: integerThe largest integer, 9223372036854775807 (2^63 - 1).
integer.minsignedAdded by Luau 0.715.
Luau
integer.minsigned: integerThe smallest integer, -9223372036854775808 (-2^63).
Arithmetic
integer.negAdded by Luau 0.715.
Luau
function integer.neg(x: integer): integerReturns -x. Negating integer.minsigned wraps around and returns integer.minsigned.
integer.addAdded by Luau 0.715.
Luau
function integer.add(x: integer, y: integer): integerReturns x + y. The result wraps around on overflow.
integer.subAdded by Luau 0.715.
Luau
function integer.sub(x: integer, y: integer): integerReturns x - y. The result wraps around on overflow.
integer.mulAdded by Luau 0.715.
Luau
function integer.mul(x: integer, y: integer): integerReturns x * y. The result wraps around on overflow.
integer.divAdded by Luau 0.715.
Luau
function integer.div(x: integer, y: integer): integerDivides x by y and rounds the quotient toward zero. A zero divisor throws division by zero and dividing integer.minsigned by -1 throws integer overflow.
print(integer.div(-7i, 2i)) --> -3
print(integer.idiv(-7i, 2i)) --> -4integer.idivAdded by Luau 0.715.
Luau
function integer.idiv(x: integer, y: integer): integerDivides x by y and rounds the quotient down, like // does for numbers. It throws in the same cases as integer.div.
integer.remAdded by Luau 0.715.
Luau
function integer.rem(x: integer, y: integer): integerReturns the remainder of integer.div(x, y). It has the sign of x. A zero divisor throws division by zero.
print(integer.rem(-7i, 2i)) --> -1
print(integer.mod(-7i, 2i)) --> 1integer.modAdded by Luau 0.715.
Luau
function integer.mod(x: integer, y: integer): integerReturns the remainder of integer.idiv(x, y). It has the sign of y like % does for numbers. A zero divisor throws division by zero.
integer.udivAdded by Luau 0.715.
Luau
function integer.udiv(x: integer, y: integer): integerDivides x by y with both read as unsigned 64-bit integers. integer.udiv(-1i, 2i) is integer.maxsigned. A zero divisor throws division by zero.
integer.uremAdded by Luau 0.715.
Luau
function integer.urem(x: integer, y: integer): integerReturns the remainder of integer.udiv(x, y). A zero divisor throws division by zero.
Comparison
integer.minAdded by Luau 0.715.
Luau
function integer.min(first: integer, ...: integer): integerReturns the smallest of its arguments. It takes at least one integer.
integer.maxAdded by Luau 0.715.
Luau
function integer.max(first: integer, ...: integer): integerReturns the largest of its arguments. It takes at least one integer.
integer.clampAdded by Luau 0.715.
Luau
function integer.clamp(x: integer, min: integer, max: integer): integerReturns x limited to the range from min to max. It throws when min is greater than max.
integer.ltAdded by Luau 0.715.
Luau
function integer.lt(x: integer, y: integer): booleanReturns whether x < y with both values read as signed 64-bit integers.
integer.leAdded by Luau 0.715.
Luau
function integer.le(x: integer, y: integer): booleanReturns whether x <= y with both values read as signed 64-bit integers.
integer.gtAdded by Luau 0.715.
Luau
function integer.gt(x: integer, y: integer): booleanReturns whether x > y with both values read as signed 64-bit integers.
integer.geAdded by Luau 0.715.
Luau
function integer.ge(x: integer, y: integer): booleanReturns whether x >= y with both values read as signed 64-bit integers.
integer.ultAdded by Luau 0.715.
Luau
function integer.ult(x: integer, y: integer): booleanReturns whether x < y with both values read as unsigned 64-bit integers.
integer.uleAdded by Luau 0.715.
Luau
function integer.ule(x: integer, y: integer): booleanReturns whether x <= y with both values read as unsigned 64-bit integers.
integer.ugtAdded by Luau 0.715.
Luau
function integer.ugt(x: integer, y: integer): booleanReturns whether x > y with both values read as unsigned 64-bit integers.
integer.ugeAdded by Luau 0.715.
Luau
function integer.uge(x: integer, y: integer): booleanReturns whether x >= y with both values read as unsigned 64-bit integers.
Bitwise operations
integer.bandAdded by Luau 0.715.
Luau
function integer.band(...: integer): integerReturns the bitwise and of its arguments. With no arguments it returns -1i (every bit set).
integer.borAdded by Luau 0.715.
Luau
function integer.bor(...: integer): integerReturns the bitwise or of its arguments. With no arguments it returns 0i.
integer.bxorAdded by Luau 0.715.
Luau
function integer.bxor(...: integer): integerReturns the bitwise exclusive or of its arguments. With no arguments it returns 0i.
integer.bnotAdded by Luau 0.715.
Luau
function integer.bnot(x: integer): integerReturns x with every bit flipped.
integer.btestAdded by Luau 0.715.
Luau
function integer.btest(...: integer): booleanReturns whether the bitwise and of its arguments isn't zero. It is equivalent to integer.band(...) ~= 0i.
Shifts and rotations
integer.lshiftAdded by Luau 0.715.
Luau
function integer.lshift(x: integer, shift: integer): integerShifts the bits of x left by shift and fills the vacated bits with zeros. A negative shift shifts right instead. A shift of 64 or more in either direction returns 0i.
integer.rshiftAdded by Luau 0.715.
Luau
function integer.rshift(x: integer, shift: integer): integerShifts the bits of x right by shift and fills the vacated bits with zeros. A negative shift shifts left instead. A shift of 64 or more in either direction returns 0i.
integer.arshiftAdded by Luau 0.715.
Luau
function integer.arshift(x: integer, shift: integer): integerShifts the bits of x right by shift and fills the vacated bits with copies of the sign bit. A negative shift shifts left instead. A right shift of 64 or more returns -1i for a negative x and 0i otherwise.
print(integer.rshift(-256i, 60i)) --> 15
print(integer.arshift(-256i, 4i)) --> -16integer.lrotateAdded by Luau 0.715.
Luau
function integer.lrotate(x: integer, shift: integer): integerRotates the bits of x left by shift. Bits that leave the top come back in at the bottom. The rotation is taken modulo 64, and a negative shift rotates right.
integer.rrotateAdded by Luau 0.715.
Luau
function integer.rrotate(x: integer, shift: integer): integerRotates the bits of x right by shift. Bits that leave the bottom come back in at the top. The rotation is taken modulo 64, and a negative shift rotates left.
Bit fields, counting and byte order
integer.extractAdded by Luau 0.715.
Luau
function integer.extract(x: integer, field: integer, width: integer?): integerReturns the width bits of x starting at bit field as an unsigned value. Bit 0 is the lowest bit and width defaults to 1. field must be from 0 to 63 and width must be positive. A field that reaches past bit 63 throws.
print(integer.extract(0xf0i, 4i, 4i)) --> 15
print(integer.replace(0i, 5i, 4i, 4i)) --> 80integer.replaceAdded by Luau 0.715.
Luau
function integer.replace(x: integer, value: integer, field: integer, width: integer?): integerReturns x with its width bits starting at bit field replaced by the low bits of value. field and width follow the same rules as in integer.extract.
integer.countlzAdded by Luau 0.715.
Luau
function integer.countlz(x: integer): integerReturns the number of zero bits above the highest set bit of x. It returns 64 for 0i.
integer.countrzAdded by Luau 0.715.
Luau
function integer.countrz(x: integer): integerReturns the number of zero bits below the lowest set bit of x. It returns 64 for 0i.
integer.bswapAdded by Luau 0.715.
Luau
function integer.bswap(x: integer): integerReverses the order of the 8 bytes of x. It converts between little endian and big endian byte order.