{"record":{"id":"dee9cf095141fdca","repo":"TheAlgorithms/C-Sharp","slug":"value-is-too-big-to-fit-into-int64","errorCode":null,"errorMessage":"Value is too big to fit into Int64","messagePattern":"Value is too big to fit into Int64","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/BitArray.cs","lineNumber":680,"sourceCode":"    public bool EvenParity() => NumberOfOneBits() % 2 == 0;\n\n    /// <summary>\n    ///     To check for odd parity.\n    /// </summary>\n    /// <returns>Returns True if parity is odd; False otherwise.</returns>\n    public bool OddParity() => NumberOfOneBits() % 2 != 0;\n\n    /// <summary>\n    ///     Returns a long integer representation of the bit-array.\n    ///     Assumes the bit-array length must been smaller or equal to 64 bit.\n    /// </summary>\n    /// <returns>Long integer array.</returns>\n    public long ToInt64()\n    {\n        // Precondition\n        if (field.Length > 64)\n        {\n            throw new InvalidOperationException(\"Value is too big to fit into Int64\");\n        }\n\n        var sequence = ToString();\n        return Convert.ToInt64(sequence, 2);\n    }\n\n    /// <summary>\n    ///     Returns a long integer representation of the bit-array.\n    ///     Assumes the bit-array length must been smaller or equal to 32 bit.\n    /// </summary>\n    /// <returns>integer array.</returns>\n    public int ToInt32()\n    {\n        // Precondition\n        if (field.Length > 32)\n        {\n            throw new InvalidOperationException(\"Value is too big to fit into Int32\");\n        }","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BitArray.cs#L662-L698","documentation":"BitArray.ToInt64() converts the bit field to a 64-bit integer. Since a long can only hold 64 bits, the method first checks that field.Length <= 64; if the BitArray was built with more than 64 bits, the value cannot fit into an Int64 and an InvalidOperationException is thrown before the conversion is attempted.","triggerScenarios":"Calling ToInt64() on a BitArray whose field.Length is greater than 64 — e.g. constructed from a binary string longer than 64 characters.","commonSituations":"Building wide BitArrays from long bit strings and then assuming every conversion method (ToInt32/ToInt64) works; calling GetHashCode() indirectly is safe (uses ToInt32) but ToInt64 is not for >64-bit fields.","solutions":["Only call ToInt64() when field.Length <= 64; check the length first.","Split BitArrays longer than 64 bits into chunks and convert each chunk separately.","Use ToString() to get the raw binary sequence for wider fields instead of forcing an Int64 conversion.","Store the value in a wider representation (e.g. BigInteger over the binary string) if >64 bits are required."],"exampleFix":"// before\nvar ba = new BitArray(new string('1', 100));\nlong v = ba.ToInt64(); // throws\n\n// after\nlong v = ba.field.Length <= 64 ? ba.ToInt64() : 0; // guard, or chunk the field","handlingStrategy":"validation","validationCode":"if (ba.field.Length > 64) throw new NotSupportedException(\"BitArray too wide for Int64\");","typeGuard":"bool CanConvertToInt64(BitArray ba) => ba.ToString().Length <= 64;","tryCatchPattern":"try { long v = ba.ToInt64(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Int64\")) { /* chunk or use string representation */ }","preventionTips":["Check field length before any ToInt64 call.","Use ToInt64 only for fields built from <=64-character sequences.","For wide fields, convert in 64-bit chunks or use BigInteger.","Document the 64-bit limit wherever BitArray is exposed."],"tags":["csharp","data-structures","bitarray","invalid-operation-exception"],"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"}