TheAlgorithms/C-Sharp · error · InvalidOperationException
Value is too big to fit into Int64
Error message
Value is too big to fit into Int64
What it means
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.
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.
Example fix
// before
var ba = new BitArray(new string('1', 100));
long v = ba.ToInt64(); // throws
// after
long v = ba.field.Length <= 64 ? ba.ToInt64() : 0; // guard, or chunk the field Defensive patterns
Strategy: validation
Validate before calling
if (ba.field.Length > 64) throw new NotSupportedException("BitArray too wide for Int64"); Type guard
bool CanConvertToInt64(BitArray ba) => ba.ToString().Length <= 64;
Try / catch
try { long v = ba.ToInt64(); } catch (InvalidOperationException ex) when (ex.Message.Contains("Int64")) { /* chunk or use string representation */ } Prevention
- 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.
When it happens
Trigger: Calling ToInt64() on a BitArray whose field.Length is greater than 64 — e.g. constructed from a binary string longer than 64 characters.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Value is too big to fit into Int32
- The sequence may only contain ones or zeros
- key is not in the tree
- Tree is empty!
- Sequence must been greater than or equal to 1
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/dee9cf095141fdca.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/BitArray.cs:680
public bool EvenParity() => NumberOfOneBits() % 2 == 0;
/// <summary>
/// To check for odd parity.
/// </summary>
/// <returns>Returns True if parity is odd; False otherwise.</returns>
public bool OddParity() => NumberOfOneBits() % 2 != 0;
/// <summary>
/// Returns a long integer representation of the bit-array.
/// Assumes the bit-array length must been smaller or equal to 64 bit.
/// </summary>
/// <returns>Long integer array.</returns>
public long ToInt64()
{
// Precondition
if (field.Length > 64)
{
throw new InvalidOperationException("Value is too big to fit into Int64");
}
var sequence = ToString();
return Convert.ToInt64(sequence, 2);
}
/// <summary>
/// Returns a long integer representation of the bit-array.
/// Assumes the bit-array length must been smaller or equal to 32 bit.
/// </summary>
/// <returns>integer array.</returns>
public int ToInt32()
{
// Precondition
if (field.Length > 32)
{
throw new InvalidOperationException("Value is too big to fit into Int32");
}View on GitHub (pinned to 96e2905cab)