The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Int.Bitwise

69 declarations and 1 deprecated aliases. Mathlib docs · Source

Bitwise operations on integers

Possibly only of archaeological significance.

Recursors

  • Int.bitCasesOn: Parity disjunction. Something is true/defined on ℤ if it's true/defined for even and for odd values.

bitwise ops

bitwise ops

theorem Int.bitwise.match_1.congr_eq_3 E1 #

∀ (motive : ℤ → ℤ → Sort u_1) (x x_1 : ℤ) (h_1 : (m n : ℕ) → motive (Int.ofNat m) (Int.ofNat n))
  (h_2 : (m n : ℕ) → motive (Int.ofNat m) (Int.negSucc n)) (h_3 : (m n : ℕ) → motive (Int.negSucc m) (Int.ofNat n))
  (h_4 : (m n : ℕ) → motive (Int.negSucc m) (Int.negSucc n)) (m n : ℕ),
  x = Int.negSucc m →
    x_1 = Int.ofNat n →
      (match x, x_1 with
        | Int.ofNat m, Int.ofNat n => h_1 m n
        | Int.ofNat m, Int.negSucc n => h_2 m n
        | Int.negSucc m, Int.ofNat n => h_3 m n
        | Int.negSucc m, Int.negSucc n => h_4 m n) ≍
        h_3 m n

theorem Int.bitwise.match_1.congr_eq_4 E1 #

∀ (motive : ℤ → ℤ → Sort u_1) (x x_1 : ℤ) (h_1 : (m n : ℕ) → motive (Int.ofNat m) (Int.ofNat n))
  (h_2 : (m n : ℕ) → motive (Int.ofNat m) (Int.negSucc n)) (h_3 : (m n : ℕ) → motive (Int.negSucc m) (Int.ofNat n))
  (h_4 : (m n : ℕ) → motive (Int.negSucc m) (Int.negSucc n)) (m n : ℕ),
  x = Int.negSucc m →
    x_1 = Int.negSucc n →
      (match x, x_1 with
        | Int.ofNat m, Int.ofNat n => h_1 m n
        | Int.ofNat m, Int.negSucc n => h_2 m n
        | Int.negSucc m, Int.ofNat n => h_3 m n
        | Int.negSucc m, Int.negSucc n => h_4 m n) ≍
        h_4 m n

theorem Int.div2.match_1.congr_eq_2 E1 #

∀ (motive : ℤ → Sort u_1) (x : ℤ) (h_1 : (n : ℕ) → motive (Int.ofNat n)) (h_2 : (n : ℕ) → motive (Int.negSucc n))
  (n : ℕ),
  x = Int.negSucc n →
    (match x with
      | Int.ofNat n => h_1 n
      | Int.negSucc n => h_2 n) ≍
      h_2 n

theorem Int.bitwise.match_1.congr_eq_2 E1 #

∀ (motive : ℤ → ℤ → Sort u_1) (x x_1 : ℤ) (h_1 : (m n : ℕ) → motive (Int.ofNat m) (Int.ofNat n))
  (h_2 : (m n : ℕ) → motive (Int.ofNat m) (Int.negSucc n)) (h_3 : (m n : ℕ) → motive (Int.negSucc m) (Int.ofNat n))
  (h_4 : (m n : ℕ) → motive (Int.negSucc m) (Int.negSucc n)) (m n : ℕ),
  x = Int.ofNat m →
    x_1 = Int.negSucc n →
      (match x, x_1 with
        | Int.ofNat m, Int.ofNat n => h_1 m n
        | Int.ofNat m, Int.negSucc n => h_2 m n
        | Int.negSucc m, Int.ofNat n => h_3 m n
        | Int.negSucc m, Int.negSucc n => h_4 m n) ≍
        h_2 m n

theorem Int.bitwise.match_1.congr_eq_1 E1 #

∀ (motive : ℤ → ℤ → Sort u_1) (x x_1 : ℤ) (h_1 : (m n : ℕ) → motive (Int.ofNat m) (Int.ofNat n))
  (h_2 : (m n : ℕ) → motive (Int.ofNat m) (Int.negSucc n)) (h_3 : (m n : ℕ) → motive (Int.negSucc m) (Int.ofNat n))
  (h_4 : (m n : ℕ) → motive (Int.negSucc m) (Int.negSucc n)) (m n : ℕ),
  x = Int.ofNat m →
    x_1 = Int.ofNat n →
      (match x, x_1 with
        | Int.ofNat m, Int.ofNat n => h_1 m n
        | Int.ofNat m, Int.negSucc n => h_2 m n
        | Int.negSucc m, Int.ofNat n => h_3 m n
        | Int.negSucc m, Int.negSucc n => h_4 m n) ≍
        h_1 m n

theorem Int.div2.match_1.congr_eq_1 E1 #

∀ (motive : ℤ → Sort u_1) (x : ℤ) (h_1 : (n : ℕ) → motive (Int.ofNat n)) (h_2 : (n : ℕ) → motive (Int.negSucc n))
  (n : ℕ),
  x = Int.ofNat n →
    (match x with
      | Int.ofNat n => h_1 n
      | Int.negSucc n => h_2 n) ≍
      h_1 n

def Int.div2 E1 #

ℤ → ℤ

div2 n = n/2

def Int.bodd E1 #

ℤ → Bool

bodd n returns true if n is odd

def Int.bit E1 #

Bool → ℤ → ℤ

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

def Int.natBitwise E1 #

(Bool → Bool → Bool) → ℕ → ℕ → ℤ

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

def Int.bitwise E1 #

(Bool → Bool → Bool) → ℤ → ℤ → ℤ

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

def Int.lnot E1 #

ℤ → ℤ

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

def Int.lor E1 #

ℤ → ℤ → ℤ

lor takes two integers and returns their bitwise or

def Int.land E1 #

ℤ → ℤ → ℤ

land takes two integers and returns their bitwise and

def Int.ldiff E1 #

ℤ → ℤ → ℤ

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.

def Int.xor E1 #

ℤ → ℤ → ℤ

xor computes the bitwise xor of two natural numbers

instance Int.instShiftLeft_mathlib E0 #

ShiftLeft ℤ

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

instance Int.instShiftRight_mathlib E0 #

ShiftRight ℤ

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

theorem Int.bodd_zero E0 #

Int.bodd 0 = false

theorem Int.bodd_one E0 #

Int.bodd 1 = true

theorem Int.bodd_two E1 #

Int.bodd 2 = false

theorem Int.bodd_coe E0 #

∀ (n : ℕ), (↑n).bodd = n.bodd

theorem Int.bodd_subNatNat E0 #

∀ (m n : ℕ), (Int.subNatNat m n).bodd = (m.bodd ^^ n.bodd)

theorem Int.bodd_negOfNat E0 #

∀ (n : ℕ), (Int.negOfNat n).bodd = n.bodd

theorem Int.bodd_neg E0 #

∀ (n : ℤ), (-n).bodd = n.bodd

theorem Int.bodd_add E0 #

∀ (m n : ℤ), (m + n).bodd = (m.bodd ^^ n.bodd)

theorem Int.bodd_mul E0 #

∀ (m n : ℤ), (m * n).bodd = (m.bodd && n.bodd)

theorem Int.bodd_add_div2 E1 #

∀ (n : ℤ), (bif n.bodd then 1 else 0) + 2 * n.div2 = n

theorem Int.div2_val E0 #

∀ (n : ℤ), n.div2 = n / 2

theorem Int.bit_val E0 #

∀ (b : Bool) (n : ℤ), Int.bit b n = 2 * n + bif b then 1 else 0

theorem Int.bit_decomp E1 #

∀ (n : ℤ), Int.bit n.bodd n.div2 = n

def Int.bitCasesOn E1 #

{C : ℤ → Sort u} → (n : ℤ) → ((b : Bool) → (n : ℤ) → C (Int.bit b n)) → C n

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

theorem Int.bit_zero E0 #

Int.bit false 0 = 0

theorem Int.bit_natCast E0 #

∀ (b : Bool) (n : ℕ), Int.bit b ↑n = ↑(Nat.bit b n)

theorem Int.bit_negSucc E0 #

∀ (b : Bool) (n : ℕ), Int.bit b (Int.negSucc n) = Int.negSucc (Nat.bit (!b) n)

theorem Int.bodd_bit E0 #

∀ (b : Bool) (n : ℤ), (Int.bit b n).bodd = b

theorem Int.testBit_bit_zero E0 #

∀ (b : Bool) (n : ℤ), (Int.bit b n).testBit 0 = b

theorem Int.testBit_bit_succ E0 #

∀ (m : ℕ) (b : Bool) (n : ℤ), (Int.bit b n).testBit m.succ = n.testBit m

theorem Int.bitwise_or E1 #

Int.bitwise or = Int.lor

theorem Int.bitwise_and E1 #

Int.bitwise and = Int.land

theorem Int.bitwise_diff E1 #

(Int.bitwise fun a b ↦ a && !b) = Int.ldiff

theorem Int.bitwise_xor E1 #

Int.bitwise xor = Int.xor

theorem Int.bitwise_bit E0 #

∀ (f : Bool → Bool → Bool) (a : Bool) (m : ℤ) (b : Bool) (n : ℤ),
  Int.bitwise f (Int.bit a m) (Int.bit b n) = Int.bit (f a b) (Int.bitwise f m n)

theorem Int.lor_bit E0 #

∀ (a : Bool) (m : ℤ) (b : Bool) (n : ℤ), (Int.bit a m).lor (Int.bit b n) = Int.bit (a || b) (m.lor n)

theorem Int.land_bit E0 #

∀ (a : Bool) (m : ℤ) (b : Bool) (n : ℤ), (Int.bit a m).land (Int.bit b n) = Int.bit (a && b) (m.land n)

theorem Int.ldiff_bit E0 #

∀ (a : Bool) (m : ℤ) (b : Bool) (n : ℤ), (Int.bit a m).ldiff (Int.bit b n) = Int.bit (a && !b) (m.ldiff n)

theorem Int.lxor_bit E0 #

∀ (a : Bool) (m : ℤ) (b : Bool) (n : ℤ), (Int.bit a m).xor (Int.bit b n) = Int.bit (a ^^ b) (m.xor n)

theorem Int.lnot_bit E0 #

∀ (b : Bool) (n : ℤ), (Int.bit b n).lnot = Int.bit (!b) n.lnot

theorem Int.testBit_bitwise E0 #

∀ (f : Bool → Bool → Bool) (m n : ℤ) (k : ℕ), (Int.bitwise f m n).testBit k = f (m.testBit k) (n.testBit k)

theorem Int.testBit_lor E0 #

∀ (m n : ℤ) (k : ℕ), (m.lor n).testBit k = (m.testBit k || n.testBit k)

theorem Int.testBit_land E0 #

∀ (m n : ℤ) (k : ℕ), (m.land n).testBit k = (m.testBit k && n.testBit k)

theorem Int.testBit_ldiff E0 #

∀ (m n : ℤ) (k : ℕ), (m.ldiff n).testBit k = (m.testBit k && !n.testBit k)

theorem Int.testBit_lxor E0 #

∀ (m n : ℤ) (k : ℕ), (m.xor n).testBit k = (m.testBit k ^^ n.testBit k)

theorem Int.testBit_lnot E0 #

∀ (n : ℤ) (k : ℕ), n.lnot.testBit k = !n.testBit k

theorem Int.shiftLeft_neg E0 #

∀ (m n : ℤ), m <<< (-n) = m >>> n

theorem Int.shiftRight_neg E0 #

∀ (m n : ℤ), m >>> (-n) = m <<< n

theorem Int.shiftLeft_natCast E0 #

∀ (m n : ℕ), ↑m <<< ↑n = ↑(m <<< n)

theorem Int.shiftRight_natCast E0 #

∀ (m n : ℕ), ↑m >>> ↑n = ↑(m >>> n)

theorem Int.shiftLeft_negSucc E0 #

∀ (m n : ℕ), Int.negSucc m <<< ↑n = Int.negSucc (Nat.shiftLeft' true m n)

theorem Int.shiftRight_negSucc E0 #

∀ (m n : ℕ), Int.negSucc m >>> ↑n = Int.negSucc (m >>> n)

theorem Int.shiftRight_add' E1 #

∀ (m : ℤ) (n k : ℕ), m >>> (↑n + ↑k) = m >>> ↑n >>> ↑k

Compare with Int.shiftRight_add, which doesn't have the coercions ℕ → ℤ.

theorem Int.shiftLeft_natCast_right E1 #

∀ (m : ℤ) (n : ℕ), m <<< ↑n = m <<< n

Connection of HShiftLeft Int Int Int and HShiftLeft Int Nat Int.

theorem Int.shiftRight_natCast_right E1 #

∀ (m : ℤ) (n : ℕ), m >>> ↑n = m >>> n

Connection of HShiftRight Int Int Int and HShiftRight Int Nat Int.

theorem Int.shiftLeft_add' E1 #

∀ (m : ℤ) (n : ℕ) (k : ℤ), m <<< (↑n + k) = m <<< ↑n <<< k

theorem Int.shiftLeft_sub E1 #

∀ (m : ℤ) (n : ℕ) (k : ℤ), m <<< (↑n - k) = m <<< ↑n >>> k

theorem Int.shiftLeft_eq_mul_pow E1 #

∀ (m : ℤ) (n : ℕ), m <<< ↑n = m * ↑(2 ^ n)

theorem Int.one_shiftLeft E1 #

∀ (n : ℕ), 1 <<< ↑n = ↑(2 ^ n)

theorem Int.zero_shiftLeft' E0 #

∀ (n : ℤ), 0 <<< n = 0

Compare with Int.zero_shiftLeft, which has n : ℕ.

theorem Int.zero_shiftRight' E0 #

∀ (n : ℤ), 0 >>> n = 0

Compare with Int.zero_shiftRight, which has n : ℕ.

Deprecated aliases

NameNew nameSince
Int.bit_coe_natInt.bit_natCast2026-10-13