This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathbitwise.lcb
More file actions
193 lines (135 loc) · 6.32 KB
/
bitwise.lcb
File metadata and controls
193 lines (135 loc) · 6.32 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
This module specifies the bitwise operations on integers included in the standard library of LiveCode Builder.
*/
module com.livecode.bitwise
use com.livecode.foreign
public foreign handler MCBitwiseEvalBitwiseAnd(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseOr(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseNot(in Operand as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseXor(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseShiftLeft(in Target as LCInt, in Shift as LCUInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseShiftRight(in Target as LCInt, in Shift as LCUInt, out Value as LCInt) returns nothing binds to "<builtin>"
--
/**
Summary: Performs a bitwise AND operation on the binary representations of <Left> and <Right>.
Left: An expression which evaluates to an integer.
Right: An expression which evaluates to an integer.
Returns: The integer whose binary representation is the result of the bitwise AND operation.
Example:
variable tVar as Number
put 3 bitwise and 6 into tVar -- tVar contains 2
Description:
Each bit of <Left> bitwise and <Right> is 1 if and only if both the corresponding bit of the binary representation of <Left> and that of <Right> is 1. Otherwise it is 0.
Tags: Bitwise operations
*/
syntax BitwiseAnd is left binary operator with bitwise and precedence
<Left: Expression> "bitwise and" <Right: Expression>
begin
MCBitwiseEvalBitwiseAnd(Left, Right, output)
end syntax
/**
Summary: Performs a bitwise OR operation on the binary representations of <Left> and <Right>.
Left: An expression which evaluates to an integer.
Right: An expression which evaluates to an integer.
Returns: The integer whose binary representation is the result of the bitwise OR operation.
Example:
variable tVar as Number
put 3 bitwise or 6 into tVar -- tVar contains 7
Description:
Each bit of <Left> bitwise or <Right> is 0 if and only if both the corresponding bit of the binary representation of <Left> and that of <Right> is 0. Otherwise it is 1.
Tags: Bitwise operations
*/
syntax BitwiseOr is left binary operator with bitwise or precedence
<Left: Expression> "bitwise or" <Right: Expression>
begin
MCBitwiseEvalBitwiseOr(Left, Right, output)
end syntax
/**
Summary: Performs a bitwise XOR operation on the binary representations of <Left> and <Right>.
Left: An expression which evaluates to an integer.
Right: An expression which evaluates to an integer.
Returns: The integer whose binary representation is the result of the bitwise XOR operation.
Example:
variable tVar as Number
put 3 bitwise xor 6 into tVar -- tVar contains 5
Description:
Each bit of <Left> bitwise xor <Right> is 1 if and only if exactly one of the corresponding bits of the binary representation of <Left> and that of <Right> is 1. Otherwise it is 0.
Tags: Bitwise operations
*/
syntax BitwiseXor is left binary operator with bitwise xor precedence
<Left: Expression> "bitwise xor" <Right: Expression>
begin
MCBitwiseEvalBitwiseXor(Left, Right, output)
end syntax
/**
Summary: Performs a bitwise NOT operation on the binary representation of <Operand>.
Operand: An expression which evaluates to an integer.
Returns: The integer whose binary representation is the result of the bitwise NOT operation.
Example:
variable tVar as Number
put bitwise not -5 into tVar -- tVar contains 4
Description:
Bitwise not returns the complement of <Operand> as a signed two's
complement integer, i.e. equivalent to -(x + 1).
Tags: Bitwise operations
*/
syntax BitwiseNot is prefix operator with modifier precedence
"bitwise" "not" <Operand: Expression>
begin
MCBitwiseEvalBitwiseNot(Operand, output)
end syntax
--
/**
Summary: Shifts the bits of <Operand> left.
Operand: An expression which evaluates to an integer.
Shift: An expression which evaluates to an integer.
Returns: The result of bit-shifting <Operand> left by <Shift> places.
Example:
variable tVar
put 7 shifted left by 2 bitwise into tVar -- tVar contains 28
Description:
Shifts the bits of <Operand> left. Shifting the bits of <Operand> left
by x is equivalent to multiplying by 2^x.
Tags: Bitwise operations
*/
syntax BitwiseShiftLeft is postfix operator with bitwise shift precedence
<Operand: Expression> "shifted" "left" "by" <Shift: Expression> "bitwise"
begin
MCBitwiseEvalBitwiseShiftLeft(Operand, Shift, output)
end syntax
/**
Summary: Shifts the bits of <Operand> right.
Operand: An expression which evaluates to an integer.
Shift: An expression which evaluates to an integer.
Returns: The result of bit-shifting <Operand> right by <Shift> places.
Example:
variable tVar
put 7 shifted right by 2 bitwise into tVar -- tVar contains 1
Description:
Shifts the bits of <Operand> right. Shifting the bits of <Operand>
right by x is equivalent to dividing by 2^x (rounding down)
Tags: Bitwise operations
*/
syntax BitwiseShiftRight is postfix operator with bitwise shift precedence
<Operand: Expression> "shifted" "right" "by" <Shift: Expression> "bitwise"
begin
MCBitwiseEvalBitwiseShiftRight(Operand, Shift, output)
end syntax
--
//syntax BitwiseRotate is postfix operator with bitwise shift precedence
// <Operand: Expression> "rotated" "by" <Shift: Expression> "bitwise"
//begin
// EvalBitwiseRotate(Operand, Shift, output)
//end syntax
end module