{"record":{"id":"fae04bc13ca9f1f8","repo":"TheAlgorithms/Java","slug":"bit-positions-must-be-between-0-and-31","errorCode":null,"errorMessage":"Bit positions must be between 0 and 31","messagePattern":"Bit positions must be between 0 and 31","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java","lineNumber":23,"sourceCode":" * This class cannot be instantiated.\n */\npublic final class BitSwap {\n    private BitSwap() {\n    }\n\n    /**\n     * Swaps two bits at specified positions in an integer.\n     *\n     * @param data The input integer whose bits need to be swapped\n     * @param posA The position of the first bit (0-based, from least significant)\n     * @param posB The position of the second bit (0-based, from least significant)\n     * @return The modified value with swapped bits\n     * @throws IllegalArgumentException if either position is negative or ≥ 32\n     */\n\n    public static int bitSwap(int data, final int posA, final int posB) {\n        if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) {\n            throw new IllegalArgumentException(\"Bit positions must be between 0 and 31\");\n        }\n\n        boolean bitA = ((data >> posA) & 1) != 0;\n        boolean bitB = ((data >> posB) & 1) != 0;\n        if (bitA != bitB) {\n            data ^= (1 << posA) ^ (1 << posB);\n        }\n        return data;\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java#L5-L34","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure both positions are in [0, 31]; remember this API is 0-based (LSB = 0).","If your input is 1-based, subtract 1 before calling.","Validate positions against Integer.SIZE (32) before invoking."],"exampleFix":"// before\nBitSwap.bitSwap(data, 1, 33);  // 1-based intent -> throws\n\n// after\n// convert 1-based to 0-based and clamp\nint a = Math.min(31, Math.max(0, posA - 1));\nint b = Math.min(31, Math.max(0, posB - 1));\nBitSwap.bitSwap(data, a, b);","handlingStrategy":"validation","validationCode":"public static boolean validBitPos(int p) {\n    return p >= 0 && p < Integer.SIZE;\n}\n// usage\nif (!validBitPos(posA) || !validBitPos(posB)) throw new IllegalArgumentException(\"positions must be 0..31\");\nBitSwap.bitSwap(data, posA, posB);","typeGuard":"public static boolean validBitPos(int p) {\n    return p >= 0 && p < Integer.SIZE;\n}","tryCatchPattern":"try {\n    return BitSwap.bitSwap(data, posA, posB);\n} catch (IllegalArgumentException e) {\n    // positions out of range; no-op\n    return data;\n}","preventionTips":["Remember this API is 0-based (LSB = 0).","Convert 1-based input to 0-based before calling.","Validate positions against Integer.SIZE (32)."],"tags":["bit-manipulation","swap","argument-validation","range-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}