BoundaryML/baml · error
bitwise & requires integer operands at
Error message
bitwise & requires integer operands at {:?} What it means
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.
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 `&`.
Example fix
// before (BAML) let ok = hasRead & hasWrite; // after let ok = hasRead && hasWrite;
Defensive patterns
Strategy: validation
Validate before calling
// check integer operands before &
if (!Number.isInteger(a) || !Number.isInteger(b)) throw new Error('bitwise & requires integer operands'); Type guard
const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
Try / catch
try {
result = evaluate(expr);
} catch (e) {
if (String(e).includes('bitwise & requires integer operands')) {
// swap to logical and if bools were intended
result = evaluate(expr.replace(' & ', ' && '));
} else throw e;
} Prevention
- Use && for boolean logic, & only for integer masks
- Normalize JSON numbers to ints before bitwise ops
- Add compile-time type checks for bitwise expressions
When it happens
Trigger: 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`.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- bitwise ^ requires integer operands at
- bitwise | requires integer operands at
- array assignment on non-list value at
- bitwise ^= requires integer operands
- bitwise |= requires integer operands
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/2981d0e1bd11101c.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-compiler/src/thir/interpret.rs:2496
_ => bail!("right operand of && must be bool at {:?}", meta.0),
},
_ => bail!("left operand of && must be bool at {:?}", meta.0),
},
BinaryOperator::Or => match left_val.clone() {
BamlValueWithMeta::Bool(true, _) => BamlValueWithMeta::Bool(true, meta.clone()),
BamlValueWithMeta::Bool(false, _) => match right_val.clone() {
BamlValueWithMeta::Bool(b, _) => BamlValueWithMeta::Bool(b, meta.clone()),
_ => bail!("right operand of || must be bool at {:?}", meta.0),
},
_ => bail!("left operand of || must be bool at {:?}", meta.0),
},
// Bitwise operations (integer only)
BinaryOperator::BitAnd => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Int(a & b, meta.clone())
}
_ => bail!("bitwise & requires integer operands at {:?}", meta.0),
},
BinaryOperator::BitOr => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Int(a | b, meta.clone())
}
_ => bail!("bitwise | requires integer operands at {:?}", meta.0),
},
BinaryOperator::BitXor => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Int(a ^ b, meta.clone())
}
_ => bail!("bitwise ^ requires integer operands at {:?}", meta.0),
},
BinaryOperator::Shl => match (left_val.clone(), right_val.clone()) {
(BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
if b < 0 {
bail!("negative shift amount at {:?}", meta.0);
}View on GitHub (pinned to bd85ce9dee)