A bit operation applies logic or shifting directly to an integer’s binary representation. In Clojure, these operations are functions commonly used for compact, efficient arithmetic and bit-container manipulation, while remaining tied to fixed-width representations. section 3.2 section 3.2.3
Use the operator family according to the shape of the operation:
- Combine bit patterns with bit-and, bit-or, bit-xor, or bit-and-not; these functions accept at least two arguments. section 3.2.3
- Inspect or modify one position with bit-test, bit-set, bit-clear, or bit-flip; the bit index starts at the least significant bit. section 3.2.3
- Move bits with bit-shift-left, bit-shift-right, or unsigned-bit-shift-right; bit-not handles a single input. section 3.2.3
- Pass byte, short, int, or long values rather than nil; invalid types raise IllegalArgumentException and nil raises NullPointerException. section 3.2.3
Masking and testing a bit
Represent binary literals with the 2r prefix. `(bit-and 2r11001001 2r11000110)` returns `192`, whose binary representation is `11000000`. To inspect the fourth bit, `(bit-test 2r11001001 3)` returns `true`; `(bit-clear 2r11001001 3)` produces `11000001`. section 3.2.3
Enumerating subsets
A bit mask can encode a subset: iterate through integers and include element `j` when `(bit-test i j)` is true. Applying this technique to `[1 2 3]` yields all eight subsets, from `()` through `(1 2 3)`. section 3.2.3
Listing 3.9. → bitwise, bit-processing
(bit-not [x]) (bit-and [x y & more]) (bit-or [x y & more]) (bit-xor [x y & more]) (bit-and-not [x y & more]) (bit-clear [x n]) (bit-set [x n]) (bit-flip [x n]) (bit-test [x n]) (bit-shift-left [x n]) (bit-shift-right [x n]) (unsigned-bit-shift-right [x n])
Browse the compact signatures for complement, combination, indexed-bit, and shifting functions. section 3.2.3





