{"record":{"id":"7bb1cf96effe5c45","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-boolean-7bb1cf","errorCode":null,"errorMessage":"Cannot apply [>>>] operation to type [boolean]","messagePattern":"Cannot apply \\[>>>\\] operation to type \\[boolean\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":1060,"sourceCode":"\n    private static int ush(int a, long b) {\n        return a >>> b;\n    }\n\n    private static long ush(long a, long b) {\n        return a >>> b;\n    }\n\n    private static float ush(float a, long b) {\n        throw new ClassCastException(\"Cannot apply [>>>] operation to type [float]\");\n    }\n\n    private static double ush(double a, long b) {\n        throw new ClassCastException(\"Cannot apply [>>>] operation to type [double]\");\n    }\n\n    private static boolean ush(boolean a, long b) {\n        throw new ClassCastException(\"Cannot apply [>>>] operation to type [boolean]\");\n    }\n\n    public static Object ush(Object left, long right) {\n        if (left instanceof Long) {\n            return (long) (left) >>> right;\n        } else {\n            return intIntegralValue(left) >>> right;\n        }\n    }\n\n    /**\n     * unboxes a class to its primitive type, or returns the original\n     * class if its not a boxed type.\n     */\n    private static Class<?> unbox(Class<?> clazz) {\n        return MethodType.methodType(clazz).unwrap().returnType();\n    }\n","sourceCodeStart":1042,"sourceCodeEnd":1078,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L1042-L1078","documentation":"Painless throws this ClassCastException at runtime when the unsigned right-shift (>>>) is applied to a boolean value through the def/dynamic path. DefMath.ush(boolean, long) is a throwing stub: booleans are not integers and bitwise shift is meaningless on them. The stub exists so the dispatcher has a registered handle for the boolean promotion class, surfacing a readable error.","triggerScenarios":"A def variable holding a boolean is shifted with >>>: 'def x = false; def y = x >>> 1;'. The dispatch through ush(Object, long) lands on the boolean stub.","commonSituations":"Scripts treating boolean doc fields as integers (assuming 0/1), generic def-based logic over heterogeneous field types, or code ported from languages that coerce booleans numerically.","solutions":["Convert the boolean to an integer explicitly: '(x ? 1 : 0) >>> 1'.","Declare the variable as an explicit integral type and assign a numeric value.","Re-examine the intent; shifting a boolean almost always indicates a type-confusion bug."],"exampleFix":"// before\ndef x = doc['enabled'].value;  // boolean\ndef y = x >>> 1;\n// after\nint x = doc['enabled'].value ? 1 : 0;\nint y = x >>> 1;","handlingStrategy":"type-guard","validationCode":"// Convert boolean def to int before >>>:\n// def x = doc['enabled'].value;\n// int n = (x instanceof Boolean) ? (((Boolean)x) ? 1 : 0) : ((Number)x).intValue();\n// int y = n >>> 1;","typeGuard":"// boolean isBool = x instanceof Boolean;","tryCatchPattern":null,"preventionTips":["Convert boolean fields to integers before bitwise operations.","Do not apply >>> to def values of unknown type.","Use explicit type declarations for known field types."],"tags":["painless","elasticsearch","type-error","bitwise","def-typing","runtime"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}