Library Coq.Numbers.Natural.Abstract.NBits
Derived properties of bitwise operations
Some properties of power and division
An injection from bits true and false to numbers 1 and 0.
We declare it as a (local) coercion for shorter statements.
We can compact testbit_odd_0 testbit_even_0
testbit_even_succ testbit_odd_succ in only two lemmas.
Alternative caracterisations of
testbit
This concise equation could have been taken as specification
for testbit in the interface, but it would have been hard to
implement with little initial knowledge about div and mod
This caracterisation that uses only basic operations and
power was initially taken as specification for testbit.
We describe a as having a low part and a high part, with
the corresponding bit in the middle. This caracterisation
is moderatly complex to implement, but also moderately
usable...
Results about the injection b2n
The specification of testbit by low and high parts is complete
All bits of number 0 are 0
Various ways to refer to the lowest bit of a number
Hence testing a bit is equivalent to shifting and testing parity
log2 gives the highest nonzero bit
Hence the number of bits of
a is
1+log2 a
(see
Pos.size_nat and
Pos.size).
Testing bits after division or multiplication by a power of two
Selecting the low part of a number can be done by a modulo
We now prove that having the same bits implies equality.
For that we use a notion of equality over functional
streams of bits.
Only zero corresponds to the always-false stream.
If two numbers produce the same stream of bits, they are equal.
The streams of bits that correspond to a natural numbers are
exactly the ones that are always 0 after some point
Properties of shifts
shifts and constants
Properties of div2.
Properties of lxor and others, directly deduced
from properties of xorb and others.
Instance lxor_wd :
Proper (
eq ==> eq ==> eq)
lxor.
Instance land_wd :
Proper (
eq ==> eq ==> eq)
land.
Instance lor_wd :
Proper (
eq ==> eq ==> eq)
lor.
Instance ldiff_wd :
Proper (
eq ==> eq ==> eq)
ldiff.
Lemma lxor_eq :
forall a a´,
lxor a a´ == 0 ->
a == a´.
Lemma lxor_nilpotent :
forall a,
lxor a a == 0.
Lemma lxor_eq_0_iff :
forall a a´,
lxor a a´ == 0
<-> a == a´.
Lemma lxor_0_l :
forall a,
lxor 0
a == a.
Lemma lxor_0_r :
forall a,
lxor a 0
== a.
Lemma lxor_comm :
forall a b,
lxor a b == lxor b a.
Lemma lxor_assoc :
forall a b c,
lxor (
lxor a b)
c == lxor a (
lxor b c).
Lemma lor_0_l :
forall a,
lor 0
a == a.
Lemma lor_0_r :
forall a,
lor a 0
== a.
Lemma lor_comm :
forall a b,
lor a b == lor b a.
Lemma lor_assoc :
forall a b c,
lor a (
lor b c)
== lor (
lor a b)
c.
Lemma lor_diag :
forall a,
lor a a == a.
Lemma lor_eq_0_l :
forall a b,
lor a b == 0 ->
a == 0.
Lemma lor_eq_0_iff :
forall a b,
lor a b == 0
<-> a == 0
/\ b == 0.
Lemma land_0_l :
forall a,
land 0
a == 0.
Lemma land_0_r :
forall a,
land a 0
== 0.
Lemma land_comm :
forall a b,
land a b == land b a.
Lemma land_assoc :
forall a b c,
land a (
land b c)
== land (
land a b)
c.
Lemma land_diag :
forall a,
land a a == a.
Lemma ldiff_0_l :
forall a,
ldiff 0
a == 0.
Lemma ldiff_0_r :
forall a,
ldiff a 0
== a.
Lemma ldiff_diag :
forall a,
ldiff a a == 0.
Lemma lor_land_distr_l :
forall a b c,
lor (
land a b)
c == land (
lor a c) (
lor b c).
Lemma lor_land_distr_r :
forall a b c,
lor a (
land b c)
== land (
lor a b) (
lor a c).
Lemma land_lor_distr_l :
forall a b c,
land (
lor a b)
c == lor (
land a c) (
land b c).
Lemma land_lor_distr_r :
forall a b c,
land a (
lor b c)
== lor (
land a b) (
land a c).
Lemma ldiff_ldiff_l :
forall a b c,
ldiff (
ldiff a b)
c == ldiff a (
lor b c).
Lemma lor_ldiff_and :
forall a b,
lor (
ldiff a b) (
land a b)
== a.
Lemma land_ldiff :
forall a b,
land (
ldiff a b)
b == 0.
Properties of setbit and clearbit
Definition setbit a n :=
lor a (1
<<n).
Definition clearbit a n :=
ldiff a (1
<<n).
Lemma setbit_spec´ :
forall a n,
setbit a n == lor a (2
^n).
Lemma clearbit_spec´ :
forall a n,
clearbit a n == ldiff a (2
^n).
Instance setbit_wd :
Proper (
eq==>eq==>eq)
setbit.
Instance clearbit_wd :
Proper (
eq==>eq==>eq)
clearbit.
Lemma pow2_bits_true :
forall n,
(2
^n).[n] = true.
Lemma pow2_bits_false :
forall n m,
n~=m ->
(2
^n).[m] = false.
Lemma pow2_bits_eqb :
forall n m,
(2
^n).[m] = eqb n m.
Lemma setbit_eqb :
forall a n m,
(setbit a n).[m] = eqb n m || a.[m].
Lemma setbit_iff :
forall a n m,
(setbit a n).[m] = true <-> n==m \/ a.[m] = true.
Lemma setbit_eq :
forall a n,
(setbit a n).[n] = true.
Lemma setbit_neq :
forall a n m,
n~=m ->
(setbit a n).[m] = a.[m].
Lemma clearbit_eqb :
forall a n m,
(clearbit a n).[m] = a.[m] && negb (
eqb n m).
Lemma clearbit_iff :
forall a n m,
(clearbit a n).[m] = true <-> a.[m] = true /\ n~=m.
Lemma clearbit_eq :
forall a n,
(clearbit a n).[n] = false.
Lemma clearbit_neq :
forall a n m,
n~=m ->
(clearbit a n).[m] = a.[m].
Shifts of bitwise operations
We cannot have a function complementing all bits of a number,
otherwise it would have an infinity of bit 1. Nonetheless,
we can design a bounded complement
Definition ones n :=
P (1
<< n).
Definition lnot a n :=
lxor a (
ones n).
Instance ones_wd :
Proper (
eq==>eq)
ones.
Instance lnot_wd :
Proper (
eq==>eq==>eq)
lnot.
Lemma ones_equiv :
forall n,
ones n == P (2
^n).
Lemma ones_add :
forall n m,
ones (
m+n)
== 2
^m * ones n + ones m.
Lemma ones_div_pow2 :
forall n m,
m<=n ->
ones n / 2
^m == ones (
n-m).
Lemma ones_mod_pow2 :
forall n m,
m<=n ->
(ones n) mod (2
^m) == ones m.
Lemma ones_spec_low :
forall n m,
m<n ->
(ones n).[m] = true.
Lemma ones_spec_high :
forall n m,
n<=m ->
(ones n).[m] = false.
Lemma ones_spec_iff :
forall n m,
(ones n).[m] = true <-> m<n.
Lemma lnot_spec_low :
forall a n m,
m<n ->
(lnot a n).[m] = negb a.[m].
Lemma lnot_spec_high :
forall a n m,
n<=m ->
(lnot a n).[m] = a.[m].
Lemma lnot_involutive :
forall a n,
lnot (
lnot a n)
n == a.
Lemma lnot_0_l :
forall n,
lnot 0
n == ones n.
Lemma lnot_ones :
forall n,
lnot (
ones n)
n == 0.
Bounded complement and other operations
Lemma lor_ones_low :
forall a n,
log2 a < n ->
lor a (
ones n)
== ones n.
Lemma land_ones :
forall a n,
land a (
ones n)
== a mod 2
^n.
Lemma land_ones_low :
forall a n,
log2 a < n ->
land a (
ones n)
== a.
Lemma ldiff_ones_r :
forall a n,
ldiff a (
ones n)
== (a >> n) << n.
Lemma ldiff_ones_r_low :
forall a n,
log2 a < n ->
ldiff a (
ones n)
== 0.
Lemma ldiff_ones_l_low :
forall a n,
log2 a < n ->
ldiff (
ones n)
a == lnot a n.
Lemma lor_lnot_diag :
forall a n,
lor a (
lnot a n)
== lor a (
ones n).
Lemma lor_lnot_diag_low :
forall a n,
log2 a < n ->
lor a (
lnot a n)
== ones n.
Lemma land_lnot_diag :
forall a n,
land a (
lnot a n)
== ldiff a (
ones n).
Lemma land_lnot_diag_low :
forall a n,
log2 a < n ->
land a (
lnot a n)
== 0.
Lemma lnot_lor_low :
forall a b n,
log2 a < n ->
log2 b < n ->
lnot (
lor a b)
n == land (
lnot a n) (
lnot b n).
Lemma lnot_land_low :
forall a b n,
log2 a < n ->
log2 b < n ->
lnot (
land a b)
n == lor (
lnot a n) (
lnot b n).
Lemma ldiff_land_low :
forall a b n,
log2 a < n ->
ldiff a b == land a (
lnot b n).
Lemma lnot_ldiff_low :
forall a b n,
log2 a < n ->
log2 b < n ->
lnot (
ldiff a b)
n == lor (
lnot a n)
b.
Lemma lxor_lnot_lnot :
forall a b n,
lxor (
lnot a n) (
lnot b n)
== lxor a b.
Lemma lnot_lxor_l :
forall a b n,
lnot (
lxor a b)
n == lxor (
lnot a n)
b.
Lemma lnot_lxor_r :
forall a b n,
lnot (
lxor a b)
n == lxor a (
lnot b n).
Lemma lxor_lor :
forall a b,
land a b == 0 ->
lxor a b == lor a b.
Bitwise operations and log2
Bitwise operations and arithmetical operations
The main result concerning addition: we express the bits of the sum
in term of bits of a and b and of some carry stream which is also
recursively determined by another equation.
Particular case : the second bit of an addition
In an addition, there will be no carries iff there is
no common bits in the numbers to add
When there is no common bits, the addition is just a xor
A null ldiff implies being smaller
Subtraction can be a ldiff when the opposite ldiff is null.
We can express lnot in term of subtraction
Adding numbers with no common bits cannot lead to a much bigger number