Documentation

TauCeti.Data.Nat.BitIndices

Set bits and binary finite-set codes #

The increasing list of set bits of a natural number can be obtained by testing every index below the number. Mathlib's Finset.equivBitIndices identifies natural numbers with finite sets of natural numbers, using ∑ x ∈ D, 2 ^ x as the code of a finite set D.

The i-th bit of the code ∑ j ∈ s, 2 ^ j records whether i ∈ s. Bitwise union, intersection and difference decode to the corresponding finite-set operations, and inserting x sets bit x. Bounded filtering keeps the requested members of a binary code, and least-element search returns none precisely for the zero code.

References #

The set bits of n are found by filtering the finite interval below n.

@[simp]
theorem Nat.testBit_sum_two_pow (s : Finset ℕ) (i : ℕ) :
(∑ j ∈ s, 2 ^ j).testBit i = decide (i ∈ s)

The i-th bit of the binary code of s records whether i ∈ s.

Binary union decodes to finite-set union.

Binary intersection decodes to finite-set intersection.

Inserting x into a finite set sets bit x of its binary code.

Binary difference decodes to finite-set difference.

def Nat.filterBitIndices (n b : ℕ) (p : ℕ → Bool) :

Keep the members of the binary-coded finite set n that lie below b and satisfy p.

Instances For

    Decoding bounded filtering gives the corresponding filtered finite set.

    @[simp]
    theorem Nat.testBit_filterBitIndices (n b x : ℕ) (p : ℕ → Bool) :
    (n.filterBitIndices b p).testBit x = (n.testBit x && decide (x < b) && p x)

    The result of bounded filtering contains exactly the requested members.

    The least member of a binary-coded finite set, or none for the empty set.

    Instances For

      Least-element search is the head of the increasing list of set bits.

      @[simp]

      Least-element search succeeds precisely at a member below every other member.

      @[simp]

      Least-element search returns none exactly for the zero code.