/
prog.go
314 lines (272 loc) · 4.14 KB
/
prog.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package ops
import (
"fmt"
"math"
"math/big"
"github.com/blackchip-org/zc/v5/pkg/zc"
)
/*
oper and
func AndBigInt p0:BigInt p1:BigInt -- BigInt
title Bitwise and
desc
The bitwise and of `p0` and `p1`
end
example
0b1100 -- 0b1100
0b1010 -- 0b1100 | 0b1010
and bin -- 0b1000
end
*/
func AndBigInt(c zc.Calc) {
var r0 big.Int
a1 := zc.PopBigInt(c)
a0 := zc.PopBigInt(c)
r0.And(a0, a1)
zc.PushBigInt(c, &r0)
}
/*
oper bin
func Bin p0:BigInt -- BigInt
title Binary conversion
desc
Convert the value of *p0* to a binary number.
end
example
0x0f -- 0x0f
bin -- 0b1111
end
*/
func Bin(c zc.Calc) {
var zero big.Int
var r0 string
a0 := zc.PopBigInt(c)
if a0.Cmp(&zero) < 0 {
var t0 big.Int
t0.Abs(a0)
r0 = fmt.Sprintf("-0b%b", &t0)
} else {
r0 = fmt.Sprintf("0b%b", a0)
}
zc.PushString(c, r0)
}
/*
oper bit
func Bit p0:BigInt n:Int -- Uint
title Bit value
desc
The value of the *n*th bit of *p0*.
end
example
0b100 -- 0b100
2 bit -- 1
end
*/
func Bit(c zc.Calc) {
i := zc.PopInt(c)
a0 := zc.PopBigInt(c)
r0 := a0.Bit(i)
zc.PushUint(c, r0)
}
/*
oper bits
func Bits p0:BigInt -- Int
title Length in bits
desc
The length of *p0* in bits.
end
example
0b11111 -- 0b11111
bits -- 5
end
*/
func Bits(c zc.Calc) {
a0 := zc.PopBigInt(c)
r0 := a0.BitLen()
zc.PushInt(c, r0)
}
/*
oper bytes
func Bytes p0:BigInt -- Int
title Length in bytes
desc
The length of *p0* in bytes.
end
example
0x1ff -- 0x1ff
bytes -- 2
end
*/
func Bytes(c zc.Calc) {
a0 := zc.PopBigInt(c)
r0 := int(math.Ceil(float64(a0.BitLen()) / 8.0))
zc.PushInt(c, r0)
}
/*
oper dec
func DecBigInt p0:BigInt -- BigInt
title Decimal conversion
desc
Convert the value of *p0* to a decimal number.
end
example
0xf -- 0xf
dec -- 15
end
*/
func DecBigInt(c zc.Calc) {
a0 := zc.PopBigInt(c)
zc.PushBigInt(c, a0)
}
/*
oper hex
func HexBigInt p0:BigInt -- BigInt
title Hexadecimal conversion
desc
Convert the value of *p0* to a hexadecimal number.
end
example
0b1111 -- 0b1111
hex -- 0xf
end
*/
func HexBigInt(c zc.Calc) {
var zero big.Int
var r0 string
a0 := zc.PopBigInt(c)
if a0.Cmp(&zero) < 0 {
var t0 big.Int
t0.Abs(a0)
r0 = fmt.Sprintf("-0x%x", &t0)
} else {
r0 = fmt.Sprintf("0x%x", a0)
}
zc.PushString(c, r0)
}
func Int(c zc.Calc) {
a0 := zc.PopBigInt(c)
zc.PushBigInt(c, a0)
}
/*
oper lsh
func Lsh p0:BigInt n:Uint -- BigInt
alias left-shift
title Left shift bits
desc
Shifts all bits in *p0* to the left by *n*.
end
example
0b10 -- 0b10
2 lsh bin -- 0b1000
end
*/
func Lsh(c zc.Calc) {
var r0 big.Int
n := zc.PopUint(c)
a0 := zc.PopBigInt(c)
r0.Lsh(a0, n)
zc.PushBigInt(c, &r0)
}
/*
oper not
func NotBigInt p0:BigInt -- BigInt
title Bitwise not
desc
The bitwise not of *p0*.
end
example
0b101 -- 0b101
not bin -- -0b110
end
*/
func NotBigInt(c zc.Calc) {
var r0 big.Int
a0 := zc.PopBigInt(c)
r0.Not(a0)
zc.PushBigInt(c, &r0)
}
/*
oper oct
func Oct p0:BigInt -- BigInt
title Octal conversion
desc
Convert the value of *p0* to an octal number.
end
example
0b1111 -- 0b1111
oct -- 0o17
end
*/
func Oct(c zc.Calc) {
var zero big.Int
var r0 string
a0 := zc.PopBigInt(c)
if a0.Cmp(&zero) < 0 {
var t0 big.Int
t0.Abs(a0)
r0 = fmt.Sprintf("-0o%o", &t0)
} else {
r0 = fmt.Sprintf("0o%o", a0)
}
zc.PushString(c, r0)
}
/*
oper or
func OrBigInt p0:BigInt p1:BigInt -- BigInt
title Bitwise or
desc
The bitwise or of *p0* and *p1*.
end
example
0b1100 -- 0b1100
0b1010 -- 0b1100 | 0b1010
or bin -- 0b1110
end
*/
func OrBigInt(c zc.Calc) {
var r0 big.Int
a1 := zc.PopBigInt(c)
a0 := zc.PopBigInt(c)
r0.Or(a0, a1)
zc.PushBigInt(c, &r0)
}
/*
oper rsh
func Rsh p0:BigInt n:Uint -- BigInt
alias right-shift
title Right shift bits
desc
Shifts all bits in *p0* to the right by *n*.
end
example
0b1000 -- 0b1000
2 rsh bin -- 0b10
end
*/
func Rsh(c zc.Calc) {
var r0 big.Int
n := zc.PopUint(c)
a0 := zc.PopBigInt(c)
r0.Rsh(a0, n)
zc.PushBigInt(c, &r0)
}
/*
oper xor
func Xor p0:BigInt p1:BigInt -- BigInt
title Bitwise exclusive or
desc
The bitwise exclusive or of `p0` and `p1`.
end
example
0b1100 -- 0b1100
0b1010 -- 0b1100 | 0b1010
xor bin -- 0b110
end
*/
func Xor(c zc.Calc) {
var r0 big.Int
a1 := zc.PopBigInt(c)
a0 := zc.PopBigInt(c)
r0.Xor(a0, a1)
zc.PushBigInt(c, &r0)
}