TheAlgorithms/Java · error · IllegalArgumentException

Bit positions must be between 0 and 31

Error message

Bit positions must be between 0 and 31

What it means

Thrown by BitSwap.bitSwap(data, posA, posB) when either bit position is negative or >= 32 (Integer.SIZE). Bit positions in a 32-bit integer are indexed 0..31 from the least significant bit; positions outside that range have no corresponding bit to swap and would produce undefined shift behavior.

Source

Thrown at src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java:23

 * This class cannot be instantiated.
 */
public final class BitSwap {
    private BitSwap() {
    }

    /**
     * Swaps two bits at specified positions in an integer.
     *
     * @param data The input integer whose bits need to be swapped
     * @param posA The position of the first bit (0-based, from least significant)
     * @param posB The position of the second bit (0-based, from least significant)
     * @return The modified value with swapped bits
     * @throws IllegalArgumentException if either position is negative or ≥ 32
     */

    public static int bitSwap(int data, final int posA, final int posB) {
        if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) {
            throw new IllegalArgumentException("Bit positions must be between 0 and 31");
        }

        boolean bitA = ((data >> posA) & 1) != 0;
        boolean bitB = ((data >> posB) & 1) != 0;
        if (bitA != bitB) {
            data ^= (1 << posA) ^ (1 << posB);
        }
        return data;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure both positions are in [0, 31]; remember this API is 0-based (LSB = 0).
  2. If your input is 1-based, subtract 1 before calling.
  3. Validate positions against Integer.SIZE (32) before invoking.

Example fix

// before
BitSwap.bitSwap(data, 1, 33);  // 1-based intent -> throws

// after
// convert 1-based to 0-based and clamp
int a = Math.min(31, Math.max(0, posA - 1));
int b = Math.min(31, Math.max(0, posB - 1));
BitSwap.bitSwap(data, a, b);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean validBitPos(int p) {
    return p >= 0 && p < Integer.SIZE;
}
// usage
if (!validBitPos(posA) || !validBitPos(posB)) throw new IllegalArgumentException("positions must be 0..31");
BitSwap.bitSwap(data, posA, posB);

Type guard

public static boolean validBitPos(int p) {
    return p >= 0 && p < Integer.SIZE;
}

Try / catch

try {
    return BitSwap.bitSwap(data, posA, posB);
} catch (IllegalArgumentException e) {
    // positions out of range; no-op
    return data;
}

Prevention

When it happens

Trigger: Calling `bitSwap(x, -1, 2)`, `bitSwap(x, 0, 32)`, or `bitSwap(x, 31, 40)`. The guard `posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE` rejects any out-of-range position. Valid positions are 0 through 31 inclusive.

Common situations: Position read as 1-based but passed as-is to a 0-based API; position computed from a length that exceeded 32; confusing this 0-based API with a 1-based bit-numbering scheme.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/fae04bc13ca9f1f8. Report an issue: GitHub.