-
Notifications
You must be signed in to change notification settings - Fork 37
/
Bitwise.tla
87 lines (73 loc) · 3.41 KB
/
Bitwise.tla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
------------------------------- MODULE Bitwise -------------------------------
LOCAL INSTANCE Integers
\* https://en.wikipedia.org/wiki/Bitwise_operation#Mathematical_equivalents
RECURSIVE And(_,_,_,_)
LOCAL And(x,y,n,m) ==
LET exp == 2^n
IN IF m = 0
THEN 0
ELSE exp * ((x \div exp) % 2) * ((y \div exp) % 2)
+ And(x,y,n+1,m \div 2)
x & y ==
(***************************************************************************)
(* Bitwise AND of (non-negative) x and y (defined for Nat \cup {0}). *)
(***************************************************************************)
IF x >= y THEN And(x, y, 0, x) ELSE And(y, x, 0, y) \* Infix variant of And(x,y)
-------------------------------------------------------------------------------
RECURSIVE Or(_,_,_,_)
LOCAL Or(x,y,n,m) ==
LET exp == 2^n
xdm == (x \div exp) % 2
ydm == (y \div exp) % 2
IN IF m = 0
THEN 0
ELSE exp * (((xdm + ydm) + (xdm * ydm)) % 2)
+ Or(x,y,n+1,m \div 2)
x | y ==
(***************************************************************************)
(* Bitwise OR of (non-negative) x and y (defined for Nat \cup {0}). *)
(***************************************************************************)
IF x >= y THEN Or(x, y, 0, x) ELSE Or(y, x, 0, y) \* Infix variant of Or(x,y)
-------------------------------------------------------------------------------
RECURSIVE Xor(_,_,_,_)
LOCAL Xor(x,y,n,m) ==
LET exp == 2^n
IN IF m = 0
THEN 0
ELSE exp * (((x \div exp) + (y \div exp)) % 2)
+ Xor(x,y,n+1,m \div 2)
x ^^ y == \* single "^" already taken by Naturals.tla
(***************************************************************************)
(* Bitwise XOR of (non-negative) x and y (defined for Nat \cup {0}). *)
(***************************************************************************)
IF x >= y THEN Xor(x, y, 0, x) ELSE Xor(y, x, 0, y) \* Infix variant of Xor(x,y)
-------------------------------------------------------------------------------
RECURSIVE NotR(_,_,_)
LOCAL NotR(x,n,m) ==
LET exp == 2^n
xdm == (x \div exp) % 2
IN IF m = 0
THEN 0
ELSE exp * ((xdm + 1) % 2)
+ NotR(x,n+1,m \div 2)
Not(a) ==
(***************************************************************************)
(* Bitwise NOT of (non-negative) x (defined for Nat \cup {0}). *)
(***************************************************************************)
NotR(a,0,a)
-------------------------------------------------------------------------------
RECURSIVE shiftR(_,_)
shiftR(n,pos) ==
(***************************************************************************)
(* Logical bit-shifting the (non-negative) n by pos positions to the right *)
(* shifting zeros in from the left/MSB (defined for Nat \cup {0}). *)
(***************************************************************************)
IF pos = 0
THEN n
ELSE LET odd(z) == z % 2 = 1
m == IF odd(n) THEN (n-1) \div 2 ELSE n \div 2
IN shiftR(m, pos - 1)
=============================================================================
\* Modification History
\* Last modified Thu April 25 10:56:12 CEST 2018 by markus
\* Created Mon May 16 15:09:18 CEST 2016 by markus