---
name: bit32
last_updated: 2026-12-04T23:21:36Z
type: library
summary: "A library of functions to perform bitwise operations."
---

# bit32

A library of functions to perform bitwise operations.

**Type:** library

## Description

This library provides functions to perform bitwise operations.

#### Number Limitations

This library treats numbers as unsigned 32-bit integers; numbers will be
converted to this before being used (see image below). Numbers with decimal
numbers are rounded to the nearest whole number.

![32-bit integer conversion (in hexadecimal)](../../../assets/engine-api/libraries/bit32/32-Bit-Restriction.png)

## Functions

### bit32.arshift

**Signature:** `bit32.arshift(x: number, disp: number): number`

Returns the number `x` shifted `disp` bits to the right. The number `disp`
may be any representable integer. Negative displacements shift to the
left.

This shift operation is what is called arithmetic shift. Vacant bits on
the left are filled with copies of the higher bit of `x`; vacant bits on
the right are filled with zeros. In particular, displacements with
absolute values higher than 31 result in zero or 0xFFFFFFFF (all original
bits are shifted out).

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bits shall be shifted. |
| `disp` | `number` |  | The integer number of bits to shift by. |

**Returns:** `number` — The result of arithmetically shifting `x` by `disp` bits to the right.

**Bit32 Arithmetic Shift**

Demonstrates the `bit32.arshift` function.

```lua
print(bit32.arshift(0x00000100, 4)) --> 16

-- The sign bit is copied into the vacated bits on the left
print(bit32.arshift(0x80000000, 4) == 0xF8000000) --> true

-- Large displacements shift every original bit out
print(bit32.arshift(0x80000000, 40) == 0xFFFFFFFF) --> true
print(bit32.arshift(0x7FFFFFFF, 40)) --> 0

-- Negative displacements shift left
print(bit32.arshift(1, -3)) --> 8
```

### bit32.band

**Signature:** `bit32.band(numbers: Tuple): number`

Returns the bitwise AND of all provided numbers.

Each bit is tested against the following truth table:

| A | B | Output |
| --- | --- | --- |
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |

![Bitwise AND of 3 numbers](../../../assets/engine-api/libraries/bit32/AND.png)

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `numbers` | `Tuple` |  | The numbers to combine with bitwise AND. |

**Returns:** `number` — The bitwise AND of all provided numbers.

**Bit32 AND**

Demonstrates the `bit32.band` function.

```lua
print(bit32.band(0xFF, 0x0F)) --> 15

-- Masking keeps only the bits set in the mask
local color = 0x336699
local green = bit32.band(bit32.rshift(color, 8), 0xFF)
print(green) --> 102

-- Any number of arguments are combined
print(bit32.band(0b1110, 0b0111, 0b0110)) --> 6
```

### bit32.bnot

**Signature:** `bit32.bnot(x: number): number`

Returns the bitwise negation of `x`.

![Negation of a provided number](../../../assets/engine-api/libraries/bit32/NOT.png)

For any integer `x`, `bit32.bnot(x) == (-1 - x) % 2^32`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number to negate. |

**Returns:** `number` — The bitwise negation of `x`.

**Bit32 NOT**

Demonstrates the `bit32.bnot` function.

```lua
print(bit32.bnot(0) == 0xFFFFFFFF) --> true
print(bit32.bnot(0xF0) == 0xFFFFFF0F) --> true

-- Negation is an involution
print(bit32.bnot(bit32.bnot(1234))) --> 1234

local x = 0xF0
print(bit32.bnot(x) == (-1 - x) % 2^32) --> true
```

### bit32.bor

**Signature:** `bit32.bor(numbers: Tuple): number`

Returns the bitwise OR of all provided numbers.

Each bit is tested against the following truth table:

| A | B | Output |
| --- | --- | --- |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 1 |

![Bitwise OR of 3 numbers](../../../assets/engine-api/libraries/bit32/OR.png)

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `numbers` | `Tuple` |  | The numbers to combine with bitwise OR. |

**Returns:** `number` — The bitwise OR of all provided numbers.

**Bit32 OR**

Demonstrates the `bit32.bor` function.

```lua
print(bit32.bor(0xF0, 0x0F)) --> 255

-- Combine flags into a single bitmask
local FLAG_READ, FLAG_WRITE, FLAG_EXEC = 1, 2, 4
local permissions = bit32.bor(FLAG_READ, FLAG_WRITE)
print(permissions) --> 3
print(bit32.bor(permissions, FLAG_EXEC)) --> 7

-- Any number of arguments are combined
print(bit32.bor(1, 2, 4, 8)) --> 15
```

### bit32.btest

**Signature:** `bit32.btest(numbers: Tuple): bool`

Returns `true` if the bitwise AND of all its operands is different from
zero, `false` otherwise. This is functionally equivalent to
`bit32.band(...) ~= 0` but returns a boolean directly without the
intermediate number.

This function is useful for testing whether one or more specific bits are
set in a value. You can check whether a particular flag is active in a
bitmask. When called with more than two arguments, all values are ANDed
together before the zero-test.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `numbers` | `Tuple` |  | The numbers to test with bitwise AND. |

**Returns:** `bool` — True if the bitwise AND of all operands is non-zero, false otherwise.

**Bit32 Test**

Demonstrates the `bit32.btest` function.

```lua
local flags = 0x5 -- bits 0 and 2 are set
print(bit32.btest(flags, 1)) --> true (bit 0 is set)
print(bit32.btest(flags, 2)) --> false (bit 1 is not set)
print(bit32.btest(flags, 4)) --> true (bit 2 is set)

-- All values are ANDed together before the zero-test
print(bit32.btest(0xFF, 0x0F, 0x03)) --> true (0xFF & 0x0F & 0x03 == 0x03)
print(bit32.btest(0xF0, 0x0F)) --> false
```

### bit32.bxor

**Signature:** `bit32.bxor(numbers: Tuple): number`

Returns the bitwise XOR of all provided numbers.

Each bit is tested against the following truth table:

| A | B | Output |
| --- | --- | --- |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 0 |

![Bitwise XOR of 3 numbers](../../../assets/engine-api/libraries/bit32/XOR.png)

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `numbers` | `Tuple` |  | The numbers to combine with bitwise XOR. |

**Returns:** `number` — The bitwise XOR of all provided numbers.

**Bit32 XOR**

Demonstrates the `bit32.bxor` function.

```lua
print(bit32.bxor(0xFF, 0x0F)) --> 240

-- XOR with the same value twice restores the original
local value, key = 0x1234, 0xABCD
local scrambled = bit32.bxor(value, key)
print(bit32.bxor(scrambled, key) == value) --> true

-- Any number of arguments are combined
print(bit32.bxor(1, 3, 7)) --> 5
```

### bit32.byteswap

**Signature:** `bit32.byteswap(x: number): number`

Reverses the byte order of the 32-bit unsigned integer representation of
`x`. The four bytes are rearranged so that the most-significant byte
becomes the least-significant byte and vice versa, converting between
big-endian and little-endian representations.

This is useful when reading or writing binary data that uses a different
byte order (endianness) than expected, such as network protocols or file
formats that store multi-byte integers in big-endian order.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bytes to swap. |

**Returns:** `number` — The number with its byte order reversed.

**Bit32 Byte Swap**

Demonstrates the `bit32.byteswap` function.

```lua
print(bit32.byteswap(0xAABBCCDD) == 0xDDCCBBAA) --> true
print(bit32.byteswap(0x00000001) == 0x01000000) --> true

-- Swapping twice restores the original value
print(bit32.byteswap(bit32.byteswap(0x12345678)) == 0x12345678) --> true
```

### bit32.countlz

**Signature:** `bit32.countlz(n: number): number`

Returns the number of consecutive zero bits in the 32-bit representation
of the provided number starting from the left-most (most significant) bit.
Returns 32 if the provided number is zero.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `n` | `number` |  | The number to count leading zeros in. |

**Returns:** `number` — The count of consecutive zero bits from the most significant bit, or
32 if `n` is zero.

**Bit32 Count Leading Zeros**

Demonstrates the `bit32.countlz` function.

```lua
print(bit32.countlz(0)) --> 32
print(bit32.countlz(1)) --> 31
print(bit32.countlz(0x80000000)) --> 0

-- Index of the highest set bit
local n = 1000
print(31 - bit32.countlz(n)) --> 9
```

### bit32.countrz

**Signature:** `bit32.countrz(n: number): number`

Returns the number of consecutive zero bits in the 32-bit representation
of the provided number starting from the right-most (least significant)
bit. Returns 32 if the provided number is zero.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `n` | `number` |  | The number to count trailing zeros in. |

**Returns:** `number` — The count of consecutive zero bits from the least significant bit, or
32 if `n` is zero.

**Bit32 Count Trailing Zeros**

Demonstrates the `bit32.countrz` function.

```lua
print(bit32.countrz(0)) --> 32
print(bit32.countrz(1)) --> 0
print(bit32.countrz(0x80000000)) --> 31

-- A power of two shifted left by n has n trailing zeros
print(bit32.countrz(bit32.lshift(1, 12))) --> 12
```

### bit32.extract

**Signature:** `bit32.extract(n: number, field: number, width?: number): number`

Returns the unsigned number formed by the bits `field` to
`field + width - 1` from `n`. Bits are numbered from 0 (least significant)
to 31 (most significant). The default for `width` is 1.

For any valid `field` and `width`, this operation is equivalent to
`bit32.band(bit32.rshift(n, field), 2^width - 1)`. `field` must be in the
range `[0, 31]`, and `width` must be in the range `[1, 32 - field]`.
Values outside those ranges raise an error.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `n` | `number` |  | The number to extract bits from. |
| `field` | `number` |  | The zero-based position of the least significant bit to extract. |
| `width` | `number` | `1` | The number of bits to extract. |

**Returns:** `number` — The unsigned number formed by the extracted bit range.

**Bit32 Extract**

Demonstrates the `bit32.extract` function.

```lua
local n = 0x12345678
print(bit32.extract(n, 0, 8) == 0x78) --> true (bits 0 through 7)
print(bit32.extract(n, 4, 8) == 0x67) --> true (bits 4 through 11)
print(bit32.extract(n, 16, 8) == 0x34) --> true (bits 16 through 23)
print(bit32.extract(n, 24, 8) == 0x12) --> true (bits 24 through 31)

-- width defaults to 1
print(bit32.extract(0x5, 2)) --> 1

-- Equivalent to shifting and masking
print(bit32.extract(n, 4, 8) == bit32.band(bit32.rshift(n, 4), 2^8 - 1)) --> true

-- Out-of-range fields raise an error
print(pcall(bit32.extract, n, 30, 8)) --> false
```

### bit32.replace

**Signature:** `bit32.replace(n: number, v: number, field: number, width?: number): number`

Returns a copy of `n` with the bits `field` to `field + width - 1`
replaced by the value `v`. See [bit32.extract()](/docs/reference/engine/globals/bit32.md) for details about
`field` and `width`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `n` | `number` |  | The number in which to replace bits. |
| `v` | `number` |  | The replacement value to insert into the bit range. |
| `field` | `number` |  | The zero-based position of the least significant bit to replace. |
| `width` | `number` | `1` | The number of bits to replace. |

**Returns:** `number` — A copy of `n` with the specified bit range replaced by `v`.

**Bit32 Replace**

Demonstrates the `bit32.replace` function.

```lua
local n = 0x12345678
print(bit32.replace(n, 0xAB, 8, 8) == 0x1234AB78) --> true

-- width defaults to 1
print(bit32.replace(0, 1, 3)) --> 8

-- The original number is not modified
print(n == 0x12345678) --> true

-- Replacing then extracting returns the inserted value
print(bit32.extract(bit32.replace(n, 0x5, 12, 4), 12, 4)) --> 5
```

### bit32.lrotate

**Signature:** `bit32.lrotate(x: number, disp: number): number`

Returns the number `x` rotated `disp` bits to the left. The number `disp`
may be any representable integer. For any valid displacement, the
following identity holds:
`bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32)`.

In particular, negative displacements rotate to the right.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bits to rotate. |
| `disp` | `number` |  | The number of bit positions to rotate left. |

**Returns:** `number` — The result of rotating `x` left by `disp` bits.

**Bit32 Left Rotate**

Demonstrates the `bit32.lrotate` function.

```lua
-- Bits shifted off the left re-enter on the right
print(bit32.lrotate(0x80000001, 1) == 0x00000003) --> true

-- Displacements wrap around modulo 32
local x, disp = 0xF0, 35
print(bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32)) --> true

-- Negative displacements rotate to the right
print(bit32.lrotate(1, -1) == 0x80000000) --> true
```

### bit32.lshift

**Signature:** `bit32.lshift(x: number, disp: number): number`

Returns the number `x` shifted `disp` bits to the left. The number `disp`
may be any representable integer. Negative displacements shift to the
right. In any direction, vacant bits are filled with zeros. In particular,
displacements with absolute values higher than 31 result in zero (all bits
are shifted out).

![Number shifted 3 to the left](../../../assets/engine-api/libraries/bit32/LSHIFT.png)

For positive displacements, the following equality holds:
`bit32.lshift(b, disp) == (b * 2^disp) % 2^32`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bits to shift. |
| `disp` | `number` |  | The number of bit positions to shift left. |

**Returns:** `number` — The result of logically shifting `x` left by `disp` bits.

**Bit32 Left Shift**

Demonstrates the `bit32.lshift` function.

```lua
print(bit32.lshift(1, 4)) --> 16

-- Bits shifted past bit 31 are discarded
print(bit32.lshift(0xFFFFFFFF, 4) == 0xFFFFFFF0) --> true

-- Large displacements shift every bit out
print(bit32.lshift(1, 32)) --> 0

-- Negative displacements shift right
print(bit32.lshift(0x100, -4)) --> 16

local b, disp = 0xF0, 3
print(bit32.lshift(b, disp) == (b * 2^disp) % 2^32) --> true
```

### bit32.rrotate

**Signature:** `bit32.rrotate(x: number, disp: number): number`

Returns the number `x` rotated `disp` bits to the right. The number `disp`
may be any representable integer.

For any valid displacement, the following identity holds:
`bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32)`.

In particular, negative displacements rotate to the left.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bits to rotate. |
| `disp` | `number` |  | The number of bit positions to rotate right. |

**Returns:** `number` — The result of rotating `x` right by `disp` bits.

**Bit32 Right Rotate**

Demonstrates the `bit32.rrotate` function.

```lua
-- Bits shifted off the right re-enter on the left
print(bit32.rrotate(1, 1) == 0x80000000) --> true

-- Displacements wrap around modulo 32
local x, disp = 0xF0, 35
print(bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32)) --> true

-- Negative displacements rotate to the left
print(bit32.rrotate(0x80000001, -1) == 0x00000003) --> true
```

### bit32.rshift

**Signature:** `bit32.rshift(x: number, disp: number): number`

Returns the number `x` shifted `disp` bits to the right. The number `disp`
may be any representable integer. Negative displacements shift to the
left. In any direction, vacant bits are filled with zeros. In particular,
displacements with absolute values higher than 31 result in zero (all bits
are shifted out).

![Number shifted 3 to the right](../../../assets/engine-api/libraries/bit32/RSHIFT.png)

For positive displacements, the following equality holds:
`bit32.rshift(b, disp) == (b % 2^32 / 2^disp) // 1`.

This shift operation is what is called logical shift.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The number whose bits to shift. |
| `disp` | `number` |  | The number of bit positions to shift right. |

**Returns:** `number` — The result of logically shifting `x` right by `disp` bits.

**Bit32 Right Shift**

Demonstrates the `bit32.rshift` function.

```lua
print(bit32.rshift(0x100, 4)) --> 16

-- Unlike arshift, the sign bit is not copied
print(bit32.rshift(0x80000000, 4) == 0x08000000) --> true

-- Large displacements shift every bit out
print(bit32.rshift(0xFFFFFFFF, 32)) --> 0

-- Negative displacements shift left
print(bit32.rshift(1, -3)) --> 8

local b, disp = 0xF00, 3
print(bit32.rshift(b, disp) == (b % 2^32 / 2^disp) // 1) --> true
```