Import (ltac.notations) BinInt.Z.
Module Z.
Local Infix ".|" := Z.lor (at level 40).
Local Infix ".&" := Z.land (at level 40).
Local Infix ".^" := Z.lxor (at level 40).
Local Notation ".~ x" := (Z.lnot x) (at level 35).
Local Notation "x .[ i ]" := (Z.testbit x i) (at level 9, format "x .[ i ]").
Local Infix "^^" := xorb (at level 40).
Local Hint Rewrite
Z.b2z_bit0
Z.bits_opp
Z.lnot_spec
Z.testbit_ones_nonneg
Z.testbit_ones
Z.add_bit0
Z.shiftl_spec
Z.shiftr_spec
using solve [trivial] : bitwise.
Local Ltac to_bitwise :=
let i := fresh "i" in
bitwise as i ?Hi;
repeat match goal with H : ?a = ?b :> Z |- _ =>
apply (f_equal (fun x => Z.testbit x i)) in H end.
Local Ltac prove_bitwise :=
apply Bool.eqb_true_iff;
repeat match goal with H : _ = _ :> bool |- _ =>
apply Bool.eqb_true_iff in H; revert H end;
rewrite <-?Bool.negb_xorb, <-?Bool.implb_true_iff, ?Bool.implb_orb;
autorewrite with bitwise;
btauto.
Local Ltac p := to_bitwise; prove_bitwise.
(** Bitwise operations alone *)

(* Specifications: *)
Lemma lnot_lnot x : Z.lnot (Z.lnot x) = x.
Lemma testbit_adccarries_0 x y c : Z.testbit (adccarries x y c) 0 = c.
Lemma testbit_addcarries_0 x y : Z.testbit (addcarries x y) 0 = false.
(** Bitwise operations, addition, and subtraction *)
Lemma sub_lor_land x y : (x .| y) - (x .& y) = (x .^ y).
Lemma sub_2landn_lxor x y : 2*(x.&.~y) - x.^y = x - y.