Documentation

Mathlib.Data.Int.Bitwise

Bitwise operations on integers #

Possibly only of archaeological significance.

Recursors #

div2 n = n/2

Equations
    Instances For
      def Int.bodd :
      โ„ค โ†’ Bool

      bodd n returns true if n is odd

      Equations
        Instances For
          def Int.bit (b : Bool) :

          bit b appends the digit b to the binary representation of its integer input.

          Equations
            Instances For
              def Int.natBitwise (f : Bool โ†’ Bool โ†’ Bool) (m n : โ„•) :

              Int.natBitwise is an auxiliary definition for Int.bitwise.

              Equations
                Instances For
                  def Int.bitwise (f : Bool โ†’ Bool โ†’ Bool) :
                  โ„ค โ†’ โ„ค โ†’ โ„ค

                  Int.bitwise applies the function f to pairs of bits in the same position in the binary representations of its inputs.

                  Equations
                    Instances For

                      lnot flips all the bits in the binary representation of its input

                      Equations
                        Instances For
                          def Int.lor :
                          โ„ค โ†’ โ„ค โ†’ โ„ค

                          lor takes two integers and returns their bitwise or

                          Equations
                            Instances For
                              def Int.land :
                              โ„ค โ†’ โ„ค โ†’ โ„ค

                              land takes two integers and returns their bitwise and

                              Equations
                                Instances For
                                  def Int.ldiff :
                                  โ„ค โ†’ โ„ค โ†’ โ„ค

                                  ldiff a b performs bitwise set difference. For each corresponding pair of bits taken as Booleans, say aแตข and bแตข, it applies the Boolean operation aแตข โˆง ยฌbแตข to obtain the iแต—สฐ bit of the result.

                                  Equations
                                    Instances For
                                      def Int.xor :
                                      โ„ค โ†’ โ„ค โ†’ โ„ค

                                      xor computes the bitwise xor of two natural numbers

                                      Equations
                                        Instances For

                                          m <<< n produces an integer whose binary representation is obtained by left-shifting the binary representation of m by n places

                                          Equations

                                            m >>> n produces an integer whose binary representation is obtained by right-shifting the binary representation of m by n places

                                            Equations

                                              bitwise ops #

                                              @[simp]
                                              theorem Int.bodd_coe (n : โ„•) :
                                              (โ†‘n).bodd = n.bodd
                                              @[simp]
                                              theorem Int.bodd_add (m n : โ„ค) :
                                              (m + n).bodd = (m.bodd ^^ n.bodd)
                                              @[simp]
                                              theorem Int.bodd_mul (m n : โ„ค) :
                                              (m * n).bodd = (m.bodd && n.bodd)
                                              theorem Int.bodd_add_div2 (n : โ„ค) :
                                              (bif n.bodd then 1 else 0) + 2 * n.div2 = n
                                              theorem Int.bit_val (b : Bool) (n : โ„ค) :
                                              bit b n = 2 * n + bif b then 1 else 0
                                              def Int.bitCasesOn {C : โ„ค โ†’ Sort u} (n : โ„ค) (h : (b : Bool) โ†’ (n : โ„ค) โ†’ C (bit b n)) :
                                              C n

                                              Defines a function from โ„ค conditionally, if it is defined for odd and even integers separately using bit.

                                              Equations
                                                Instances For
                                                  @[simp]
                                                  theorem Int.bit_coe_nat (b : Bool) (n : โ„•) :
                                                  bit b โ†‘n = โ†‘(Nat.bit b n)
                                                  @[simp]
                                                  theorem Int.bit_negSucc (b : Bool) (n : โ„•) :
                                                  bit b (negSucc n) = negSucc (Nat.bit (!b) n)
                                                  @[simp]
                                                  theorem Int.bodd_bit (b : Bool) (n : โ„ค) :
                                                  (bit b n).bodd = b
                                                  @[simp]
                                                  theorem Int.testBit_bit_zero (b : Bool) (n : โ„ค) :
                                                  (bit b n).testBit 0 = b
                                                  @[simp]
                                                  theorem Int.testBit_bit_succ (m : โ„•) (b : Bool) (n : โ„ค) :
                                                  (bit b n).testBit m.succ = n.testBit m
                                                  @[simp]
                                                  theorem Int.bitwise_bit (f : Bool โ†’ Bool โ†’ Bool) (a : Bool) (m : โ„ค) (b : Bool) (n : โ„ค) :
                                                  bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n)
                                                  @[simp]
                                                  theorem Int.lor_bit (a : Bool) (m : โ„ค) (b : Bool) (n : โ„ค) :
                                                  (bit a m).lor (bit b n) = bit (a || b) (m.lor n)
                                                  @[simp]
                                                  theorem Int.land_bit (a : Bool) (m : โ„ค) (b : Bool) (n : โ„ค) :
                                                  (bit a m).land (bit b n) = bit (a && b) (m.land n)
                                                  @[simp]
                                                  theorem Int.ldiff_bit (a : Bool) (m : โ„ค) (b : Bool) (n : โ„ค) :
                                                  (bit a m).ldiff (bit b n) = bit (a && !b) (m.ldiff n)
                                                  @[simp]
                                                  theorem Int.lxor_bit (a : Bool) (m : โ„ค) (b : Bool) (n : โ„ค) :
                                                  (bit a m).xor (bit b n) = bit (a ^^ b) (m.xor n)
                                                  @[simp]
                                                  theorem Int.lnot_bit (b : Bool) (n : โ„ค) :
                                                  (bit b n).lnot = bit (!b) n.lnot
                                                  @[simp]
                                                  theorem Int.testBit_bitwise (f : Bool โ†’ Bool โ†’ Bool) (m n : โ„ค) (k : โ„•) :
                                                  (bitwise f m n).testBit k = f (m.testBit k) (n.testBit k)
                                                  @[simp]
                                                  theorem Int.testBit_lor (m n : โ„ค) (k : โ„•) :
                                                  (m.lor n).testBit k = (m.testBit k || n.testBit k)
                                                  @[simp]
                                                  theorem Int.testBit_land (m n : โ„ค) (k : โ„•) :
                                                  (m.land n).testBit k = (m.testBit k && n.testBit k)
                                                  @[simp]
                                                  theorem Int.testBit_ldiff (m n : โ„ค) (k : โ„•) :
                                                  (m.ldiff n).testBit k = (m.testBit k && !n.testBit k)
                                                  @[simp]
                                                  theorem Int.testBit_lxor (m n : โ„ค) (k : โ„•) :
                                                  (m.xor n).testBit k = (m.testBit k ^^ n.testBit k)
                                                  @[simp]
                                                  theorem Int.shiftLeft_neg (m n : โ„ค) :
                                                  m <<< (-n) = m >>> n
                                                  @[simp]
                                                  theorem Int.shiftRight_neg (m n : โ„ค) :
                                                  m >>> (-n) = m <<< n
                                                  @[simp]
                                                  theorem Int.shiftLeft_natCast (m n : โ„•) :
                                                  โ†‘m <<< โ†‘n = โ†‘(m <<< n)
                                                  @[simp]
                                                  theorem Int.shiftRight_natCast (m n : โ„•) :
                                                  โ†‘m >>> โ†‘n = โ†‘(m >>> n)
                                                  @[simp]
                                                  theorem Int.shiftRight_negSucc (m n : โ„•) :
                                                  negSucc m >>> โ†‘n = negSucc (m >>> n)
                                                  theorem Int.shiftRight_add' (m : โ„ค) (n k : โ„•) :
                                                  m >>> (โ†‘n + โ†‘k) = m >>> โ†‘n >>> โ†‘k

                                                  Compare with Int.shiftRight_add, which doesn't have the coercions โ„• โ†’ โ„ค.

                                                  bitwise ops #

                                                  theorem Int.shiftLeft_add' (m : โ„ค) (n : โ„•) (k : โ„ค) :
                                                  m <<< (โ†‘n + k) = m <<< โ†‘n <<< k
                                                  theorem Int.shiftLeft_sub (m : โ„ค) (n : โ„•) (k : โ„ค) :
                                                  m <<< (โ†‘n - k) = m <<< โ†‘n >>> k
                                                  theorem Int.shiftLeft_eq_mul_pow (m : โ„ค) (n : โ„•) :
                                                  m <<< โ†‘n = m * โ†‘(2 ^ n)
                                                  theorem Int.one_shiftLeft (n : โ„•) :
                                                  1 <<< โ†‘n = โ†‘(2 ^ n)
                                                  @[simp]
                                                  theorem Int.zero_shiftLeft' (n : โ„ค) :
                                                  0 <<< n = 0

                                                  Compare with Int.zero_shiftLeft, which has n : โ„•.

                                                  @[simp]
                                                  theorem Int.zero_shiftRight' (n : โ„ค) :
                                                  0 >>> n = 0

                                                  Compare with Int.zero_shiftRight, which has n : โ„•.