{"record":{"id":"2981d0e1bd11101c","repo":"BoundaryML/baml","slug":"bitwise-requires-integer-operands-at","errorCode":null,"errorMessage":"bitwise & requires integer operands at {:?}","messagePattern":"bitwise & requires integer operands at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2496,"sourceCode":"                _ => bail!(\"right operand of && must be bool at {:?}\", meta.0),\n            },\n            _ => bail!(\"left operand of && must be bool at {:?}\", meta.0),\n        },\n        BinaryOperator::Or => match left_val.clone() {\n            BamlValueWithMeta::Bool(true, _) => BamlValueWithMeta::Bool(true, meta.clone()),\n            BamlValueWithMeta::Bool(false, _) => match right_val.clone() {\n                BamlValueWithMeta::Bool(b, _) => BamlValueWithMeta::Bool(b, meta.clone()),\n                _ => bail!(\"right operand of || must be bool at {:?}\", meta.0),\n            },\n            _ => bail!(\"left operand of || must be bool at {:?}\", meta.0),\n        },\n\n        // Bitwise operations (integer only)\n        BinaryOperator::BitAnd => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a & b, meta.clone())\n            }\n            _ => bail!(\"bitwise & requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::BitOr => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a | b, meta.clone())\n            }\n            _ => bail!(\"bitwise | requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::BitXor => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a ^ b, meta.clone())\n            }\n            _ => bail!(\"bitwise ^ requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::Shl => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                if b < 0 {\n                    bail!(\"negative shift amount at {:?}\", meta.0);\n                }","sourceCodeStart":2478,"sourceCodeEnd":2514,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2478-L2514","documentation":"BAML's bitwise AND operator is defined only for integer operands. evaluate_binary_op pattern-matches both operands as BamlValueWithMeta::Int and bails with this message if either side is any other value type.","triggerScenarios":"Evaluating `a & b` where either operand is a bool, float, string, or null — e.g. `flags & 0b1010` where flags is bool, or `1.5 & 3`.","commonSituations":"Applying C/Java-style `&` as logical-and on bools (which BAML forbids — use `&&`); bit flags stored as floats after JSON parsing; a string-encoded mask value from config.","solutions":["Use `&&` instead of `&` for logical conjunction of booleans.","Ensure both operands are integers; cast/round floats before bitwise ops.","Parse string-encoded masks with an explicit int conversion before `&`."],"exampleFix":"// before (BAML)\nlet ok = hasRead & hasWrite;\n// after\nlet ok = hasRead && hasWrite;","handlingStrategy":"validation","validationCode":"// check integer operands before &\nif (!Number.isInteger(a) || !Number.isInteger(b)) throw new Error('bitwise & requires integer operands');","typeGuard":"const isInt = (v) => typeof v === 'number' && Number.isInteger(v);","tryCatchPattern":"try {\n  result = evaluate(expr);\n} catch (e) {\n  if (String(e).includes('bitwise & requires integer operands')) {\n    // swap to logical and if bools were intended\n    result = evaluate(expr.replace(' & ', ' && '));\n  } else throw e;\n}","preventionTips":["Use && for boolean logic, & only for integer masks","Normalize JSON numbers to ints before bitwise ops","Add compile-time type checks for bitwise expressions"],"tags":["baml","type-mismatch","interpreter","bitwise-operator"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}