liveBook · Manning

Bit-and

As explained in Clojure: The Essential Reference

bit-and is a Clojure function that applies bitwise conjunction to two or more integer arguments.

bit-and applies bitwise conjunction to two or more integer arguments. Each output bit remains 1 only when the corresponding bit is 1 in every argument, making it useful for masking and low-level arithmetic. section 3.2.3

Use the following contract when calling bit-and:

  • Pass at least two byte, short, int, or long values; additional arguments are allowed. section 3.2.3
  • Receive a java.lang.Long containing the bitwise conjunction. section 3.2.3
  • Expect IllegalArgumentException for an invalid type and NullPointerException for nil. section 3.2.3

Combining binary masks

(bit-and 201 198) ;; 192 (Long/toBinaryString (bit-and 2r11001001 2r11000110)) ;; "11000000" (bit-and 2r11001001 2r11001000 2r01011110) ;; 01000000

How Clojure: The Essential Reference covers it

How deep it goes
Defines accepted types, variadic arity, return type, exceptions, and binary examples without descending into JVM implementation details.
Examples
Demonstrates decimal operands, binary literals, multi-argument conjunction, and masks that isolate or clear a bit.
What it assumes
Assumes familiarity with integers and binary notation while establishing bit containers and bitwise behavior.
What you can do after
Apply it via Clojure to combine integer bit patterns and construct masks for selected bits.
How it connects
Connects bit-and with bitwise masking, bit-or, bit-xor, bit-and-not, bit-test, and bit shifting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.