aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stdlib/source/lux/data/bit.lux26
-rw-r--r--stdlib/test/test/lux/data/bit.lux10
2 files changed, 14 insertions, 22 deletions
diff --git a/stdlib/source/lux/data/bit.lux b/stdlib/source/lux/data/bit.lux
index 4e87a23dc..29b01e370 100644
--- a/stdlib/source/lux/data/bit.lux
+++ b/stdlib/source/lux/data/bit.lux
@@ -1,5 +1,7 @@
(;module: [lux #- and or not])
+(def: #export width Nat +64)
+
## [Values]
(do-template [<short-name> <op> <doc> <type>]
[(def: #export (<short-name> param subject)
@@ -7,18 +9,12 @@
(-> Nat <type> <type>)
(_lux_proc ["bit" <op>] [subject param]))]
- [and "and"
- "Bitwise and." Nat]
- [or "or"
- "Bitwise or." Nat]
- [xor "xor"
- "Bitwise xor." Nat]
- [shift-left "shift-left"
- "Bitwise shift-left." Nat]
- [shift-right "shift-right"
- "Bitwise shift-right." Int]
- [unsigned-shift-right "unsigned-shift-right"
- "Bitwise unsigned-shift-right." Nat]
+ [and "and" "Bitwise and." Nat]
+ [or "or" "Bitwise or." Nat]
+ [xor "xor" "Bitwise xor." Nat]
+ [shift-left "shift-left" "Bitwise shift-left." Nat]
+ [shift-right "shift-right" "Bitwise shift-right." Int]
+ [unsigned-shift-right "unsigned-shift-right" "Bitwise unsigned-shift-right." Nat]
)
(def: #export (count subject)
@@ -52,14 +48,12 @@
(-> Nat Nat Bool)
(|> input (;;and (shift-left idx +1)) (n.= +0) ;not))
-(def: rot-top Nat +64)
-
(do-template [<name> <main> <comp>]
[(def: #export (<name> distance input)
(-> Nat Nat Nat)
(;;or (<main> distance input)
- (<comp> (n.- (n.% rot-top distance)
- rot-top)
+ (<comp> (n.- (n.% width distance)
+ width)
input)))]
[rotate-left shift-left unsigned-shift-right]
diff --git a/stdlib/test/test/lux/data/bit.lux b/stdlib/test/test/lux/data/bit.lux
index 2ba2c8e57..fe04806cd 100644
--- a/stdlib/test/test/lux/data/bit.lux
+++ b/stdlib/test/test/lux/data/bit.lux
@@ -7,11 +7,9 @@
["R" math/random])
lux/test)
-(def: width Nat +64)
-
(test: "Bitwise operations."
[pattern R;nat
- idx (:: @ map (n.% width) R;nat)]
+ idx (:: @ map (n.% &;width) R;nat)]
($_ seq
(assert "Clearing and settings bits should alter the count."
(and (n.< (&;count (&;set idx pattern))
@@ -31,7 +29,7 @@
(and (not (&;set? idx pattern))
(&;set? idx (&;flip idx pattern))))))
(assert "The negation of a bit pattern should have a complementary bit count."
- (n.= width
+ (n.= &;width
(n.+ (&;count pattern)
(&;count (&;not pattern)))))
(assert "Can do simple binary boolean logic."
@@ -58,10 +56,10 @@
(n.= pattern))))
(assert "Rotate as many spaces as the bit-pattern's width leaves the pattern unchanged."
(and (|> pattern
- (&;rotate-left width)
+ (&;rotate-left &;width)
(n.= pattern))
(|> pattern
- (&;rotate-right width)
+ (&;rotate-right &;width)
(n.= pattern))))
(assert "Shift right respect the sign of ints."
(let [value (nat-to-int pattern)]