Documentation

Hale.Base.Data.Bits

class Data.Bits (α : Type u) :

Typeclass for types supporting bitwise operations.

$$\text{Bits}(\alpha)$$ requires at minimum: bitwise AND, OR, XOR, complement, shifts, bit testing, and a notion of zero bits.

Corresponds to Haskell's Data.Bits.Bits.

  • and : ααα

    Bitwise AND. $$a \mathbin{\&} b$$

  • or : ααα

    Bitwise OR. $$a \mathbin{|} b$$

  • xor : ααα

    Bitwise XOR. $$a \oplus b$$

  • complement : αα

    Bitwise complement. $$\sim a$$

  • shiftL : αNatα

    Left shift by $n$ bits. $$a \ll n$$

  • shiftR : αNatα

    Right shift by $n$ bits. $$a \gg n$$

  • testBit : αNatBool

    Test bit at position $n$ (zero-indexed from LSB). $$\text{testBit}(a, n) = ((a \gg n) \mathbin{\&} 1) \neq 0$$

  • bit : Natα

    The value with only bit $n$ set. $$\text{bit}(n) = 1 \ll n$$

  • popCount : αNat

    Count the number of set bits (population count). $$\text{popCount}(a) = |\{i \mid \text{testBit}(a, i)\}|$$

  • zeroBits : α

    The all-zeros value. $$\text{zeroBits} = 0$$

  • bitSizeMaybe : Option Nat

    Bit size if fixed-width, none for arbitrary-width. $$\text{bitSizeMaybe} \in \{\text{none}\} \cup \{\text{some}(n) \mid n \in \mathbb{N}\}$$

Instances
    class Data.FiniteBits (α : Type u) extends Data.Bits α :

    Typeclass for fixed-width bit types, extending Bits.

    $$\text{FiniteBits}(\alpha)$$ adds finiteBitSize, countLeadingZeros, and countTrailingZeros.

    Instances
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[inline]
      def Data.Bits.setBit {α : Type u_1} [Bits α] (a : α) (n : Nat) :
      α

      Set a specific bit to 1. $$\text{setBit}(a, n) = a \mathbin{|} \text{bit}(n)$$

      Equations
      Instances For
        @[inline]
        def Data.Bits.clearBit {α : Type u_1} [Bits α] (a : α) (n : Nat) :
        α

        Clear a specific bit to 0. $$\text{clearBit}(a, n) = a \mathbin{\&} \sim\text{bit}(n)$$

        Equations
        Instances For
          @[inline]
          def Data.Bits.complementBit {α : Type u_1} [Bits α] (a : α) (n : Nat) :
          α

          Toggle a specific bit. $$\text{complementBit}(a, n) = a \oplus \text{bit}(n)$$

          Equations
          Instances For