{"record":{"id":"8ca7c42565163fd3","repo":"TheAlgorithms/C-Sharp","slug":"sequence-must-be-not-longer-than-the-bit-array-length","errorCode":null,"errorMessage":"sequence must be not longer than the bit array length","messagePattern":"sequence must be not longer than the bit array length","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/BitArray.cs","lineNumber":536,"sourceCode":"    ///     The input assumes arrays have the same length.\n    /// </summary>\n    /// <param name=\"one\">First bit-array.</param>\n    /// <param name=\"two\">Second bit-array.</param>\n    /// <returns>Returns True if there inputs aren't equal; False otherwise.</returns>\n    public static bool operator !=(BitArray one, BitArray two) => !(one == two);\n\n    /// <summary>\n    ///     Compiles the binary sequence into the inner data structure.\n    ///     The sequence must have the same length, as the bit-array.\n    ///     The sequence may only be allowed contains ones or zeros.\n    /// </summary>\n    /// <param name=\"sequence\">A string sequence of 0's and 1's.</param>\n    public void Compile(string sequence)\n    {\n        // precondition I\n        if (sequence.Length > field.Length)\n        {\n            throw new ArgumentException($\"{nameof(sequence)} must be not longer than the bit array length\");\n        }\n\n        // precondition II\n        ThrowIfSequenceIsInvalid(sequence);\n\n        // for appropriate scaling\n        if (sequence.Length < field.Length)\n        {\n            var difference = field.Length - sequence.Length;\n            sequence = new string('0', difference) + sequence;\n        }\n\n        // actual compile procedure.\n        for (var i = 0; i < sequence.Length; i++)\n        {\n            field[i] = sequence[i] == '1';\n        }\n    }","sourceCodeStart":518,"sourceCodeEnd":554,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BitArray.cs#L518-L554","documentation":"BitArray.Compile(string) throws ArgumentException when the given sequence is longer than the array's underlying field length. Compile overwrites the bit array starting at index 0, so a sequence longer than the allocated bits cannot fit; the caller must construct an array at least as long as the sequence.","triggerScenarios":"`bitArray.Compile(sequence)` where sequence.Length > field.Length, e.g., compiling a longer binary string into a BitArray built with a smaller size or from a shorter initial string.","commonSituations":"Reusing one BitArray instance across inputs of varying length; resizing input data without reallocating the array; hardcoded array sizes that a longer config/probe bit string outgrows.","solutions":["Recreate the BitArray with a size >= sequence.Length before compiling: `new BitArray(sequence.Length)` then Compile","Check `sequence.Length <= bitArray.Length` (or the backing field length) before compiling","Trim or validate input sequences to a fixed expected width","Catch ArgumentException and rebuild a larger array as a fallback"],"exampleFix":"// before\nshared.Compile(sequence); // throws if sequence longer than array\n// after\nif (sequence.Length > shared.Length)\n{\n    shared = new BitArray(sequence);\n}\nelse\n{\n    shared.Compile(sequence);\n}","handlingStrategy":"validation","validationCode":"if (sequence.Length > bitArray.Length) bitArray = new BitArray(sequence.Length);","typeGuard":"static bool FitsInArray(BitArray a, string seq) => seq.Length <= a.Length && seq.All(c => c == '0' || c == '1');","tryCatchPattern":"try { bitArray.Compile(sequence); } catch (ArgumentException) { /* sequence too long or invalid */ }","preventionTips":["Size the BitArray to the longest expected sequence","Validate sequence length before Compile","Avoid sharing one BitArray instance across variable-length inputs"],"tags":["csharp","bitarray","length-mismatch"],"backgroundTag":"value-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}