Skip to content

Commit 5958bac

Browse files
committed
feat(math): add new bitwise builtins
1 parent 3b4823c commit 5958bac

2 files changed

Lines changed: 107 additions & 1 deletion

File tree

‎Math.ark‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,87 @@
158158
# @author https://github.com/SuperFola
159159
(let NaN builtin__math:NaN)
160160

161+
# @brief Count the number of '1' bits in the given value
162+
# @param value the Number, must be an integer
163+
# =begin
164+
# (math:countOnes 5) # 2
165+
# =end
166+
# @author https://github.com/SuperFola
167+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
168+
(let countOnes (fun (_x) (builtin__math:countOnes _x)))
169+
170+
# @brief Count the number of '0' bits in the given value
171+
# @param value the Number, must be an integer
172+
# =begin
173+
# (math:countZeros 5) # 1
174+
# =end
175+
# @author https://github.com/SuperFola
176+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
177+
(let countZeros (fun (_x) (builtin__math:countZeros _x)))
178+
179+
# @brief Invert a number on 64 bits
180+
# @param value Number, must be an integer
181+
# =begin
182+
# (math:bitNot 5) # -6
183+
# =end
184+
# @author https://github.com/SuperFola
185+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
186+
(let bitNot (fun (_x) (builtin__math:bitNot _x)))
187+
188+
# @brief Bitwise and
189+
# @param lhs Number, must be an integer
190+
# @param rhs Number, must be an integer
191+
# =begin
192+
# (math:bitAnd 4 12) # 4
193+
# (math:bitAnd 4 10) # 0
194+
# =end
195+
# @author https://github.com/SuperFola
196+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
197+
(let bitAnd (fun (_a _b) (builtin__math:bitAnd _a _b)))
198+
199+
# @brief Bitwise or
200+
# @param lhs Number, must be an integer
201+
# @param rhs Number, must be an integer
202+
# =begin
203+
# (math:bitOr 4 10) # 14
204+
# (math:bitOr 4 1) # 5
205+
# =end
206+
# @author https://github.com/SuperFola
207+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
208+
(let bitOr (fun (_a _b) (builtin__math:bitOr _a _b)))
209+
210+
# @brief Bitwise exclusive or of lhs and rhs
211+
# @param lhs Number, must be an integer
212+
# @param rhs Number, must be an integer
213+
# =begin
214+
# (math:bitXor 4 12) # 8
215+
# (math:bitXor 4 10) # 14
216+
# =end
217+
# @author https://github.com/SuperFola
218+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
219+
(let bitXor (fun (_a _b) (builtin__math:bitXor _a _b)))
220+
221+
# @brief Left bit shift
222+
# @param lhs Number, must be an integer
223+
# @param rhs Number, must be an integer
224+
# =begin
225+
# (math:lshift 3 2) # 12
226+
# =end
227+
# @author https://github.com/SuperFola
228+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
229+
(let lshift (fun (_a _b) (builtin__math:lshift _a _b)))
230+
231+
# @brief Right bit shift
232+
# @param lhs Number, must be an integer
233+
# @param rhs Number, must be an integer
234+
# =begin
235+
# (math:rshift 13 2) # 3
236+
# (math:rshift -14 2) # -4
237+
# =end
238+
# @author https://github.com/SuperFola
239+
# @require 4.6.0 Requires ArkScript 4.6.0 or later
240+
(let rshift (fun (_a _b) (builtin__math:rshift _a _b)))
241+
161242
# @brief Return the absolute value of a number
162243
# @param _x the number to get the absolute value of
163244
# @author https://github.com/rstefanic

‎tests/math-tests.ark‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,32 @@
2929
(test:eq (builtin__math:tanh 5) (math:tanh 5))
3030
(test:eq (builtin__math:acosh 5) (math:acosh 5))
3131
(test:eq (builtin__math:asinh 5) (math:asinh 5))
32-
(test:eq (builtin__math:atanh 0.5) (math:atanh 0.5)) })
32+
(test:eq (builtin__math:atanh 0.5) (math:atanh 0.5))
33+
(test:eq (builtin__math:countOnes 1234) (math:countOnes 1234))
34+
(test:eq (builtin__math:countZeros 1234) (math:countZeros 1234))
35+
(test:eq (builtin__math:bitNot 1234) (math:bitNot 1234))
36+
(test:eq (builtin__math:bitAnd 1234 5678) (math:bitAnd 1234 5678))
37+
(test:eq (builtin__math:bitOr 1234 5678) (math:bitOr 1234 5678))
38+
(test:eq (builtin__math:bitXor 1234 5678) (math:bitXor 1234 5678))
39+
(test:eq (builtin__math:lshift 1234 2) (math:lshift 1234 2))
40+
(test:eq (builtin__math:rshift 1234 6) (math:rshift 1234 6)) })
41+
42+
(test:case "bitwise" {
43+
(test:eq (math:countOnes 5) 2)
44+
(test:eq (math:countOnes 63) 6)
45+
(test:eq (math:countZeros 63) 58)
46+
(test:eq (math:countZeros 65535) 48)
47+
48+
(test:eq (math:bitNot 5) -6)
49+
(test:eq (math:bitAnd 4 12) 4)
50+
(test:eq (math:bitAnd 4 10) 0)
51+
(test:eq (math:bitOr 4 10) 14)
52+
(test:eq (math:bitOr 4 1) 5)
53+
(test:eq (math:bitXor 4 12) 8)
54+
(test:eq (math:bitXor 4 10) 14)
55+
(test:eq (math:lshift 3 2) 12)
56+
(test:eq (math:rshift 13 2) 3)
57+
(test:eq (math:rshift -14 2) -4) })
3358

3459
(test:case "abs" {
3560
(test:eq (math:abs -1) 1)

0 commit comments

Comments
 (0)