×ばつ callback/generator). Useful for pattern matching with extraction, constraint search, type inference, planners, expert systems, and policy checks. ESM, single runtime dependency (deep6). - rules bits · uhop/yopl Wiki"> ×ばつ callback/generator). Useful for pattern matching with..." /> ×ばつ callback/generator). Useful for pattern matching with..." />×ばつ callback/generator). Useful for pattern matching with..." />
Skip to content

Navigation Menu

Sign in
Sign up

rules bits

Eugene Lazutkin edited this page May 8, 2026 · 3 revisions

rules-bits

Bitwise rule library.

Import

import {rules as bitsRules} from 'yopl/rules/bits.js';

Predicates

bitAnd(X, Y, Z)X & Y = Z

Forward-only: requires X and Y to be bound; computes Z. With all three bound, behaves as a check. Cannot be solved backwards because bitwise AND is not invertible (many (X, Y) pairs produce the same Z).

const Z = variable('Z');
solve(bitsRules, 'bitAnd', [0b1100, 0b1010, Z], env => {
 console.log(assemble(Z, env).toString(2)); // 1000
});

bitOr(X, Y, Z)X | Y = Z

Forward-only, same constraints as bitAnd. OR is also non-invertible.

bitXor(X, Y, Z)X ^ Y = Z

Reversible. XOR is its own inverse (X ^ Y ^ Y = X), so any one missing operand can be solved for. Includes shortcut clauses 0 ^ Y = Y, X ^ 0 = X, X ^ X = 0.

// Solve for Y: 0b1100 ^ Y = 0b0110
const Y = variable('Y');
solve(bitsRules, 'bitXor', [0b1100, Y, 0b0110], env => {
 console.log(assemble(Y, env).toString(2)); // 1010
});

bitNot(X, Y)Y = ~X

Reversible bitwise NOT. Either argument may be unbound and is solved for; with both bound the rule behaves as a check.

Use cases

Bitwise predicates are most useful when integrating with code that already speaks bit flags — masking, packing, or parsing fixed-width records:

const rules = {
 ...systemRules,
 ...bitsRules,
 // hasFlag(Flags, Mask) — true if all bits in Mask are set in Flags
 hasFlag: (Flags, Mask) => [head(Flags, Mask), term('bitAnd', Flags, Mask, Mask)]
};

For pure-arithmetic work, prefer the rules-math library — bitwise tricks are clever but harder to read.

Limitations

  • Operands must be plain JavaScript numbers. The native &, |, ^, ~ operators coerce to 32-bit signed integers, so values outside that range are truncated.
  • bitAnd and bitOr are not reversible.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /