BabylonJS/Babylon.js · error · RangeError

Bit index out of range

Error message

Bit index out of range

What it means

BitArray.get() validates that the requested bit index is within the array's declared size before computing the byte offset and bitmask. Reading at or beyond size would read bytes outside the backing array, so the library throws a RangeError instead of returning garbage.

Source

Thrown at packages/dev/core/src/Misc/bitArray.ts:32

export class BitArray {
    private readonly _byteArray: Uint8Array;

    /**
     * Creates a new bit array with a fixed size.
     * @param size The number of bits to store.
     */
    public constructor(public readonly size: number) {
        this._byteArray = new Uint8Array(Math.ceil(this.size / 8));
    }

    /**
     * Gets the current value at the specified index.
     * @param bitIndex The index to get the value from.
     * @returns The value at the specified index.
     */
    public get(bitIndex: number): boolean {
        if (bitIndex >= this.size) {
            throw new RangeError("Bit index out of range");
        }
        const byteIndex = GetByteIndex(bitIndex);
        const bitMask = GetBitMask(bitIndex);
        return (this._byteArray[byteIndex] & bitMask) !== 0;
    }

    /**
     * Sets the value at the specified index.
     * @param bitIndex The index to set the value at.
     * @param value The value to set.
     */
    public set(bitIndex: number, value: boolean): void {
        if (bitIndex >= this.size) {
            throw new RangeError("Bit index out of range");
        }
        const byteIndex = GetByteIndex(bitIndex);
        const bitMask = GetBitMask(bitIndex);
        if (value) {

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Check the index against bitArray.size before calling get (valid indexes are 0..size-1)
  2. Fix loop bounds to use `for (let i = 0; i < bitArray.size; i++)`
  3. Construct/resize the BitArray with a size large enough for all indexes you will read

Example fix

// before
const value = bitArray.get(index); // index may equal size
// after
const value = index < bitArray.size ? bitArray.get(index) : false;
Defensive patterns

Strategy: validation

Validate before calling

if (bitIndex < 0 || bitIndex >= bitArray.size) throw new RangeError(`bitIndex ${bitIndex} outside 0..${bitArray.size - 1}`);

Type guard

function inRange(bits: { size: number }, i: number): boolean {
  return Number.isInteger(i) && i >= 0 && i < bits.size;
}

Try / catch

try {
  return bitArray.get(bitIndex);
} catch (e) {
  if (e instanceof RangeError) {
    console.warn(`Bit index ${bitIndex} >= size ${bitArray.size}; returning false`);
    return false;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling bitArray.get(i) where i >= bitArray.size — e.g. indexing with a loop bound from a different array, or a BitArray constructed with too small a size for the data being read.

Common situations: Off-by-one loop conditions (i <= size instead of i < size); deserializing data written with a larger BitArray; sharing one index variable across differently sized BitArrays.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/ce7cbe2143869d59. Report an issue: GitHub.