TheAlgorithms/C-Sharp · error · ArgumentException
number must be positive
Error message
number must be positive
What it means
BitArray.Compile(int) throws ArgumentException when the number is zero or negative. Only positive integers have a well-defined unsigned binary representation for this implementation, so the method rejects number <= 0 as its first precondition before converting to binary with Convert.ToString(number, 2).
Solutions
- Validate `number > 0` before calling Compile and handle zero/negative explicitly
- If zero must be representable, write the bits manually (e.g., compile the string "0") rather than using Compile(int)
- Catch ArgumentException when caller-supplied values are untrusted
- Clamp or map non-positive inputs to a valid domain before invoking
Example fix
// before
bits.Compile(count); // throws when count == 0
// after
if (count > 0)
{
bits.Compile(count);
}
else
{
bits.Compile("0"); // explicit zero representation
} Defensive patterns
Strategy: validation
Validate before calling
if (number > 0) bitArray.Compile(number); else /* handle zero/negative */;
Type guard
static bool IsCompilable(int n) => n > 0 && Convert.ToString(n, 2).Length <= bitArray.Length;
Try / catch
try { bitArray.Compile(number); } catch (ArgumentException) { /* non-positive or too big */ } Prevention
- Reject non-positive values at the input boundary
- Initialize counters before compiling
- Document Compile(int) as positive-only in callers
When it happens
Trigger: `bitArray.Compile(0)` or `bitArray.Compile(negativeInt)` — commonly from counter variables that start at 0, default/uninitialized int fields, or signed subtraction results.
Common situations: Passing an accumulator that never incremented; feeding a difference (a-b) that came out negative; default(struct) initialization yielding 0; parsing user input that permits zero.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Sequence must been greater than or equal to 1
- sequence must be not longer than the bit array length
- Provided number is too big
- Value is too big to fit into Int64
- Value is too big to fit into Int32
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/0b1c819e46ca80d6.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/BitArray.cs:566
// actual compile procedure.
for (var i = 0; i < sequence.Length; i++)
{
field[i] = sequence[i] == '1';
}
}
/// <summary>
/// Compiles integer number into the inner data structure.
/// Assumes: the number must have the same bit length.
/// </summary>
/// <param name="number">A positive integer number.</param>
public void Compile(int number)
{
// precondition I
if (number <= 0)
{
throw new ArgumentException($"{nameof(number)} must be positive");
}
// converts to binary representation
var binaryNumber = Convert.ToString(number, 2);
// precondition II
if (binaryNumber.Length > field.Length)
{
throw new ArgumentException("Provided number is too big");
}
// for appropriate scaling
if (binaryNumber.Length < field.Length)
{
var difference = field.Length - binaryNumber.Length;
binaryNumber = new string('0', difference) + binaryNumber;
}
View on GitHub (pinned to 96e2905cab)