Documentation

Mathlib.Data.Num.Bitwise

Bitwise operations using binary representation of integers #

Definitions #

Bitwise "or" for PosNum.

Instances For
    @[implicit_reducible]
    instance PosNum.instOrOp :
    OrOp PosNum
    @[simp]
    theorem PosNum.lor_eq_or (p q : PosNum) :
    p.lor q = p ||| q

    Bitwise "and" for PosNum.

    Instances For
      @[implicit_reducible]
      @[simp]
      theorem PosNum.land_eq_and (p q : PosNum) :
      p.land q = p &&& q

      Bitwise fun a b ↦ a && !b for PosNum. For example, ldiff 5 9 = 4:

       101
      1001
      ----
       100
      
      Instances For

        Bitwise "xor" for PosNum.

        Instances For
          @[implicit_reducible]
          @[simp]
          theorem PosNum.lxor_eq_xor (p q : PosNum) :
          p.lxor q = p ^^^ q
          def PosNum.testBit :
          PosNumBool

          a.testBit n is true iff the n-th bit (starting from the LSB) in the binary representation of a is active. If the size of a is less than n, this evaluates to false.

          Instances For
            def PosNum.oneBits :
            PosNumList

            n.oneBits 0 is the list of indices of active bits in the binary representation of n.

            Instances For
              def PosNum.shiftl :
              PosNumPosNum

              Left-shift the binary representation of a PosNum.

              Instances For
                @[implicit_reducible]
                instance PosNum.instHShiftLeftNat :
                HShiftLeft PosNum PosNum
                @[simp]
                theorem PosNum.shiftl_eq_shiftLeft (p : PosNum) (n : ) :
                p.shiftl n = p <<< n
                theorem PosNum.shiftl_succ_eq_bit0_shiftl (p : PosNum) (n : ) :
                p <<< n.succ = (p <<< n).bit0
                def PosNum.shiftr :
                PosNumNum

                Right-shift the binary representation of a PosNum.

                Instances For
                  @[implicit_reducible]
                  instance PosNum.instHShiftRightNatNum :
                  HShiftRight PosNum Num
                  @[simp]
                  theorem PosNum.shiftr_eq_shiftRight (p : PosNum) (n : ) :
                  p.shiftr n = p >>> n
                  def Num.lor :
                  NumNumNum

                  Bitwise "or" for Num.

                  Instances For
                    @[implicit_reducible]
                    instance Num.instOrOp :
                    OrOp Num
                    @[simp]
                    theorem Num.lor_eq_or (p q : Num) :
                    p.lor q = p ||| q
                    def Num.land :
                    NumNumNum

                    Bitwise "and" for Num.

                    Instances For
                      @[implicit_reducible]
                      instance Num.instAndOp :
                      AndOp Num
                      @[simp]
                      theorem Num.land_eq_and (p q : Num) :
                      p.land q = p &&& q
                      def Num.ldiff :
                      NumNumNum

                      Bitwise fun a b ↦ a && !b for Num. For example, ldiff 5 9 = 4:

                       101
                      1001
                      ----
                       100
                      
                      Instances For
                        def Num.lxor :
                        NumNumNum

                        Bitwise "xor" for Num.

                        Instances For
                          @[implicit_reducible]
                          instance Num.instXorOp :
                          XorOp Num
                          @[simp]
                          theorem Num.lxor_eq_xor (p q : Num) :
                          p.lxor q = p ^^^ q
                          def Num.shiftl :
                          NumNum

                          Left-shift the binary representation of a Num.

                          Instances For
                            @[implicit_reducible]
                            instance Num.instHShiftLeftNat :
                            HShiftLeft Num Num
                            @[simp]
                            theorem Num.shiftl_eq_shiftLeft (p : Num) (n : ) :
                            p.shiftl n = p <<< n
                            def Num.shiftr :
                            NumNum

                            Right-shift the binary representation of a Num.

                            Instances For
                              @[implicit_reducible]
                              instance Num.instHShiftRightNat :
                              HShiftRight Num Num
                              @[simp]
                              theorem Num.shiftr_eq_shiftRight (p : Num) (n : ) :
                              p.shiftr n = p >>> n
                              def Num.testBit :
                              NumBool

                              a.testBit n is true iff the n-th bit (starting from the LSB) in the binary representation of a is active. If the size of a is less than n, this evaluates to false.

                              Instances For
                                def Num.oneBits :
                                NumList

                                n.oneBits is the list of indices of active bits in the binary representation of n.

                                Instances For
                                  inductive NzsNum :

                                  This is a nonzero (and "non minus one") version of SNum. See the documentation of SNum for more details.

                                  Instances For
                                    @[implicit_reducible]
                                    instance instDecidableEqNzsNum :
                                    DecidableEq NzsNum
                                    def instDecidableEqNzsNum.decEq (x✝ x✝¹ : NzsNum) :
                                    Decidable (x✝ = x✝¹)
                                    Instances For
                                      inductive SNum :

                                      Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to msb denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement.

                                      13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb true)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb false))))

                                      As with Num, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the Bool field indicates the sign of the number.

                                      0 = ..0000000(base 2) = zero false -1 = ..1111111(base 2) = zero true

                                      Instances For
                                        def instDecidableEqSNum.decEq (x✝ x✝¹ : SNum) :
                                        Decidable (x✝ = x✝¹)
                                        Instances For
                                          @[implicit_reducible]
                                          instance instDecidableEqSNum :
                                          DecidableEq SNum
                                          @[implicit_reducible]
                                          @[implicit_reducible]
                                          instance instZeroSNum :
                                          Zero SNum
                                          @[implicit_reducible]
                                          instance instOneNzsNum :
                                          One NzsNum
                                          @[implicit_reducible]
                                          instance instOneSNum :
                                          One SNum
                                          @[implicit_reducible]
                                          instance instInhabitedNzsNum :
                                          Inhabited NzsNum
                                          @[implicit_reducible]
                                          instance instInhabitedSNum :
                                          Inhabited SNum

                                          The SNum representation uses a bit string, essentially a list of 0 (false) and 1 (true) bits, and the negation of the MSB is sign-extended to all higher bits.

                                          def NzsNum.«term_::_» :
                                          Lean.TrailingParserDescr

                                          Add a bit at the end of a NzsNum.

                                          Instances For
                                            def NzsNum.sign :
                                            NzsNumBool

                                            Sign of a NzsNum.

                                            Instances For
                                              @[match_pattern]

                                              Bitwise not for NzsNum.

                                              Instances For
                                                def NzsNum.«term~_» :
                                                Lean.ParserDescr

                                                Bitwise not for NzsNum.

                                                Instances For

                                                  Add an inactive bit at the end of a NzsNum. This mimics PosNum.bit0.

                                                  Instances For

                                                    Add an active bit at the end of a NzsNum. This mimics PosNum.bit1.

                                                    Instances For
                                                      def NzsNum.head :
                                                      NzsNumBool

                                                      The head of a NzsNum is the Boolean value of its LSB.

                                                      Instances For

                                                        The tail of a NzsNum is the SNum obtained by removing the LSB. Edge cases: tail 1 = 0 and tail (-2) = -1.

                                                        Instances For
                                                          def SNum.sign :
                                                          SNumBool

                                                          Sign of a SNum.

                                                          Instances For
                                                            @[match_pattern]
                                                            def SNum.not :

                                                            Bitwise not for SNum.

                                                            Instances For
                                                              def SNum.«term~_» :
                                                              Lean.ParserDescr

                                                              Bitwise not for SNum.

                                                              Instances For
                                                                @[match_pattern]
                                                                def SNum.bit :
                                                                BoolSNumSNum

                                                                Add a bit at the end of a SNum. This mimics NzsNum.bit.

                                                                Instances For
                                                                  def SNum.«term_::_» :
                                                                  Lean.TrailingParserDescr

                                                                  Add a bit at the end of a SNum. This mimics NzsNum.bit.

                                                                  Instances For

                                                                    Add an inactive bit at the end of a SNum. This mimics ZNum.bit0.

                                                                    Instances For

                                                                      Add an active bit at the end of a SNum. This mimics ZNum.bit1.

                                                                      Instances For
                                                                        theorem SNum.bit_zero (b : Bool) :
                                                                        bit b (zero b) = zero b
                                                                        theorem SNum.bit_one (b : Bool) :
                                                                        bit b (zero (decide ¬b = true)) = nz (NzsNum.msb b)
                                                                        def NzsNum.drec' {C : SNumSort u_1} (z : (b : Bool) → C (SNum.zero b)) (s : (b : Bool) → (p : SNum) → C pC (SNum.bit b p)) (p : NzsNum) :
                                                                        C (SNum.nz p)

                                                                        A dependent induction principle for NzsNum, with base cases 0 : SNum and (-1) : SNum.

                                                                        Instances For
                                                                          def SNum.head :
                                                                          SNumBool

                                                                          The head of a SNum is the Boolean value of its LSB.

                                                                          Instances For

                                                                            The tail of a SNum is obtained by removing the LSB. Edge cases: tail 1 = 0, tail (-2) = -1, tail 0 = 0 and tail (-1) = -1.

                                                                            Instances For
                                                                              def SNum.drec' {C : SNumSort u_1} (z : (b : Bool) → C (zero b)) (s : (b : Bool) → (p : SNum) → C pC (bit b p)) (p : SNum) :
                                                                              C p

                                                                              A dependent induction principle for SNum which avoids relying on NzsNum.

                                                                              Instances For
                                                                                def SNum.rec' {α : Sort u_1} (z : Boolα) (s : BoolSNumαα) :
                                                                                SNumα

                                                                                An induction principle for SNum which avoids relying on NzsNum.

                                                                                Instances For
                                                                                  def SNum.testBit :
                                                                                  SNumBool

                                                                                  SNum.testBit n a is true iff the n-th bit (starting from the LSB) of a is active. If the size of a is less than n, this evaluates to false.

                                                                                  Instances For

                                                                                    The successor of a SNum (i.e. the operation adding one).

                                                                                    Instances For

                                                                                      The predecessor of a SNum (i.e. the operation of removing one).

                                                                                      Instances For
                                                                                        def SNum.neg (n : SNum) :

                                                                                        The opposite of a SNum.

                                                                                        Instances For
                                                                                          @[implicit_reducible]
                                                                                          instance SNum.instNeg :
                                                                                          Neg SNum
                                                                                          def SNum.czAdd :
                                                                                          BoolBoolSNumSNum

                                                                                          SNum.czAdd a b n is n + a - b (where a and b should be read as either 0 or 1). This is useful to implement the carry system in cAdd.

                                                                                          Instances For
                                                                                            def SNum.bits :
                                                                                            SNum(n : ) → List.Vector Bool n

                                                                                            a.bits n is the vector of the n first bits of a (starting from the LSB).

                                                                                            Instances For
                                                                                              def SNum.cAdd :
                                                                                              SNumSNumBoolSNum

                                                                                              SNum.cAdd n m a is n + m + a (where a should be read as either 0 or 1). a represents a carry bit.

                                                                                              Instances For
                                                                                                def SNum.add (a b : SNum) :

                                                                                                Add two SNums.

                                                                                                Instances For
                                                                                                  @[implicit_reducible]
                                                                                                  instance SNum.instAdd :
                                                                                                  Add SNum
                                                                                                  def SNum.sub (a b : SNum) :

                                                                                                  Subtract two SNums.

                                                                                                  Instances For
                                                                                                    @[implicit_reducible]
                                                                                                    instance SNum.instSub :
                                                                                                    Sub SNum
                                                                                                    def SNum.mul (a : SNum) :

                                                                                                    Multiply two SNums.

                                                                                                    Instances For
                                                                                                      @[implicit_reducible]
                                                                                                      instance SNum.instMul :
                                                                                                      Mul SNum