antlr/antlr4 · error · ArgumentOutOfRangeException

nbits

Error message

nbits

What it means

Error "nbits" thrown in antlr/antlr4.

Source

Thrown at runtime/CSharp/src/Sharpen/BitSet.cs:24

{
    using System;
    using System.Text;

    public class BitSet
    {
        private static readonly ulong[] EmptyBits = Collections.EmptyList<ulong>();
        private const int BitsPerElement = 8 * sizeof(ulong);

        private ulong[] _data = EmptyBits;

        public BitSet()
        {
        }

        public BitSet(int nbits)
        {
            if (nbits < 0)
                throw new ArgumentOutOfRangeException("nbits");

            if (nbits > 0)
            {
                int length = (nbits + BitsPerElement - 1) / BitsPerElement;
                _data = new ulong[length];
            }
        }

        private static int GetBitCount(ulong[] value)
        {
            int data = 0;
            uint size = (uint)value.Length;
            const ulong m1 = 0x5555555555555555;
            const ulong m2 = 0x3333333333333333;
            const ulong m4 = 0x0F0F0F0F0F0F0F0F;
            const ulong m8 = 0x00FF00FF00FF00FF;
            const ulong m16 = 0x0000FFFF0000FFFF;
            const ulong h01 = 0x0101010101010101;

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Pass a non-negative bit count when constructing the BitSet.
  2. Validate the size parameter before allocating the set.

Example fix

var bits = new BitSet(64);

When it happens

Trigger: BitSet constructed with a negative nbits argument.

Common situations: Pass a non-negative bit count when constructing Sharpen.BitSet.


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/31fc90d4aaa19c7f. Report an issue: GitHub.