{"record":{"id":"05535b0d06c28b8f","repo":"TheAlgorithms/Java","slug":"negative-numbers-are-not-allowed","errorCode":null,"errorMessage":"Negative numbers are not allowed.","messagePattern":"Negative numbers are not allowed\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/EvilNumber.java","lineNumber":33,"sourceCode":"    // Function to count number of one bits in a number using bitwise operators\n    private static int countOneBits(int number) {\n        int oneBitCounter = 0;\n        while (number > 0) {\n            oneBitCounter += number & 1; // increment count if last bit is 1\n            number >>= 1; // right shift to next bit\n        }\n        return oneBitCounter;\n    }\n\n    /**\n     * Check either {@code number} is an Evil number or Odious number\n     *\n     * @param number the number\n     * @return {@code true} if {@code number} is an Evil number, otherwise false (in case of of Odious number)\n     */\n    public static boolean isEvilNumber(int number) {\n        if (number < 0) {\n            throw new IllegalArgumentException(\"Negative numbers are not allowed.\");\n        }\n\n        int noOfOneBits = countOneBits(number);\n        return noOfOneBits % 2 == 0;\n    }\n}\n","sourceCodeStart":15,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/EvilNumber.java#L15-L40","documentation":"Thrown by EvilNumber.isEvilNumber when number < 0. An Evil number has an even count of 1-bits in its binary representation; the bit-counting is defined for the non-negative integer representation. Negative Java ints have a two's-complement representation whose bit count is not meaningful for this classification, so the library rejects negatives before counting.","triggerScenarios":"Calling isEvilNumber(-1), isEvilNumber(-15), or passing an unvalidated value. The guard fires before countOneBits is invoked.","commonSituations":"User input accepting negative numbers; arithmetic that underflows; default values; using the method in a loop that includes negative indices.","solutions":["Pass a non-negative integer (>= 0) such as isEvilNumber(15).","Validate at the caller: if (number < 0) reject.","Use Math.abs only if negative input is truly equivalent in your domain (note: this changes the bit pattern, so prefer rejecting)."],"exampleFix":"// before\nboolean e = EvilNumber.isEvilNumber(-1);\n\n// after\nboolean e = EvilNumber.isEvilNumber(15);","handlingStrategy":"validation","validationCode":"if (number < 0) {\n    throw new IllegalArgumentException(\"Evil number check requires >= 0\");\n}\nEvilNumber.isEvilNumber(number);","typeGuard":"static boolean isNonNegative(int n) { return n >= 0; }","tryCatchPattern":null,"preventionTips":["Reject negatives before bit-count classification.","Do not use Math.abs to 'fix' negatives; it changes the bit pattern and the result.","Validate user input that may carry a minus sign."],"tags":["validation","number-theory","bit-manipulation","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}