liveBook · Manning

Bit operation

As explained in Clojure: The Essential Reference

A bitwise operator performs a logical or shifting operation directly on the binary representation of an integer.

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

How Clojure: The Essential Reference covers it

How deep it goes
Defines operator families, contracts, exceptions, binary examples, signed shifts, and a powerset technique without detailing hardware internals.
Examples
Demonstrates binary conversion, masking, bit testing, clearing, shifting signed values, and powerset generation.
What it assumes
Assumes familiarity with integers and binary notation while teaching Clojure function arities, bit indexing, masks, and result types.
What you can do after
Apply Clojure bitwise functions to combine, inspect, modify, shift, and encode integer bit patterns.
How it connects
Connects bit operations with binary representation, bit masks, arithmetic, shifting, CPU registers, and powersets.

Also explained in

Compare how 6 books explain bit operation →

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.