> Agent-readable docs index: /llms.txt. Download /docs.zip to grep all markdown files locally.

---
title: bit
description: LuaJIT bitwise operation library
icon: cpu
---

The `bit` namespace exposes the LuaJIT bitwise operation library.

## arshift

```lua
bit.arshift(x: number, n: number): number
```

> Returns the bitwise arithmetic right-shift of its first argument by the number of bits given by the second argument. Arithmetic right-shift treats the most-significant bit as a sign bit and replicates it. Only the lower 5 bits of the shift count are used (reduces to the range \[0..31]).

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of bits.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise arithmetic right-shift result.
  </SchemaField>
</Schema>

## band

```lua
bit.band(x1: number, x2: number[, ...]): number
```

> Returns the bitwise and of all of its arguments. Note that more than two arguments are allowed.

<Schema title="Parameters">
  <SchemaField name="x1" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="x2" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="..." type="any">
    Number(s).
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise and of all arguments.
  </SchemaField>
</Schema>

## bnot

```lua
bit.bnot(x: number): number
```

> Returns the bitwise not of its argument.

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise not of the argument.
  </SchemaField>
</Schema>

## bor

```lua
bit.bor(x1: number, x2: number[, ...]): number
```

> Returns the bitwise or of all of its arguments. Note that more than two arguments are allowed.

<Schema title="Parameters">
  <SchemaField name="x1" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="x2" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="..." type="any">
    Number(s).
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise or of all arguments.
  </SchemaField>
</Schema>

## bswap

```lua
bit.bswap(x: number): number
```

> Swaps the bytes of its argument and returns it. This can be used to convert little-endian 32 bit numbers to big-endian 32 bit numbers or vice versa.

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The argument with its bytes swapped.
  </SchemaField>
</Schema>

## bxor

```lua
bit.bxor(x1: number, [x2...]: number): number
```

> Returns the bitwise xor of all of its arguments. Note that more than two arguments are allowed.

<Schema title="Parameters">
  <SchemaField name="x1" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="[x2...]" type="number">
    Number(s).
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise xor of all arguments.
  </SchemaField>
</Schema>

## lshift

```lua
bit.lshift(x: number, n: number): number
```

> Returns the bitwise logical left-shift of its first argument by the number of bits given by the second argument. Logical shifts treat the first argument as an unsigned number and shift in 0-bits. Only the lower 5 bits of the shift count are used (reduces to the range \[0..31]).

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of bits.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise logical left-shift result.
  </SchemaField>
</Schema>

## rol

```lua
bit.rol(x: number, n: number): number
```

> Returns the bitwise left rotation of its first argument by the number of bits given by the second argument. Bits shifted out on one side are shifted back in on the other side. Only the lower 5 bits of the rotate count are used (reduces to the range \[0..31]).

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of bits.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise left rotation result.
  </SchemaField>
</Schema>

## ror

```lua
bit.ror(x: number, n: number): number
```

> Returns the bitwise right rotation of its first argument by the number of bits given by the second argument. Bits shifted out on one side are shifted back in on the other side. Only the lower 5 bits of the rotate count are used (reduces to the range \[0..31]).

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of bits.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise right rotation result.
  </SchemaField>
</Schema>

## rshift

```lua
bit.rshift(x: number, n: number): number
```

> Returns the bitwise logical right-shift of its first argument by the number of bits given by the second argument. Logical shifts treat the first argument as an unsigned number and shift in 0-bits. Only the lower 5 bits of the shift count are used (reduces to the range \[0..31]).

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of bits.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The bitwise logical right-shift result.
  </SchemaField>
</Schema>

## tobit

```lua
bit.tobit(x: number): number
```

> Normalizes a number to the numeric range for bit operations and returns it. This function is usually not needed since all bit operations already normalize all of their input arguments.

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number to normalize.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The normalized number.
  </SchemaField>
</Schema>

## tohex

```lua
bit.tohex(x: number, n: number): number
```

> Converts its first argument to a hex string. The number of hex digits is given by the absolute value of the optional second argument. Positive numbers between 1 and 8 generate lowercase hex digits. Negative numbers generate uppercase hex digits. Only the least-significant 4\*|n| bits are used. The default is to generate 8 lowercase hex digits.

<Schema title="Parameters">
  <SchemaField name="x" type="number" required>
    Number to convert.
  </SchemaField>

  <SchemaField name="n" type="number" required>
    Number of hex digits to return.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="result" type="number">
    The hex string representation.
  </SchemaField>
</Schema>
