BoundaryML/baml · error

bitwise ^ requires integer operands at

Error message

bitwise ^ requires integer operands at {:?}

What it means

THIR interpreter runtime error: the bitwise `^` (xor) operator was applied to non-integer operands. Only Int/Int is defined for bitwise ops, so any other operand pair falls through to this bail at the expression's location.

Solutions

  1. For boolean XOR, use `(a && !b) || (!a && b)` or a comparison instead of `^`.
  2. If exponentiation was intended, use the language's power function/multiplication, not `^`.
  3. Coerce operands to integers explicitly before bitwise XOR.

Example fix

// before (BAML)
let flipped = flag ^ 1;
// after
let flipped = !flag;
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')) {
    // likely wanted boolean xor or exponentiation
    result = evaluate(rewriteXor(expr));
  } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `a ^ b` where either operand is bool, float, string, or null — e.g. `toggle ^ 1` with a bool toggle, or XOR-ing hashed values stored as strings.

Common situations: Using `^` as exponentiation (as in some languages) — BAML does not define it that way; toggling bool flags with `^` instead of `!=`/`&&`/`||` logic; float inputs from JSON.

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/6aac981c3f96b015. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-compiler/src/thir/interpret.rs:2508

        // 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);
                }
                BamlValueWithMeta::Int(a << b, meta.clone())
            }
            _ => bail!("shift << requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::Shr => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b < 0 {
                    bail!("negative shift amount at {:?}", meta.0);
                }
                BamlValueWithMeta::Int(a >> b, meta.clone())
            }
            _ => bail!("shift >> requires integer operands at {:?}", meta.0),

View on GitHub (pinned to bd85ce9dee)