Skip to content

Commit 2f33b6d

Browse files
authored
Merge pull request #2 from Proclivis/master
Add logic functions.
2 parents ce4614c + e6dcfa1 commit 2f33b6d

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

docs/Data/BigInt.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,54 @@ prime :: BigInt -> Boolean
7878

7979
Returns `true` if the number is prime, `false` otherwise.
8080

81+
#### `not`
82+
83+
``` purescript
84+
not :: BigInt -> BigInt
85+
```
86+
87+
Returns a bit inverted number.
88+
89+
#### `or`
90+
91+
``` purescript
92+
or :: BigInt -> BigInt -> BigInt
93+
```
94+
95+
Returns a bit or'ed number.
96+
97+
#### `xor`
98+
99+
``` purescript
100+
xor :: BigInt -> BigInt -> BigInt
101+
```
102+
103+
Returns a bit xor'ed numnber.
104+
105+
#### `and`
106+
107+
``` purescript
108+
and :: BigInt -> BigInt -> BigInt
109+
```
110+
111+
Returns a bit and'ed number.
112+
113+
#### `shl`
114+
115+
``` purescript
116+
shl :: BigInt -> Number -> BigInt
117+
```
118+
119+
Shift the first number to the right by the second number of bits and return the result. Shifts in ones if the first number is negative.
120+
121+
#### `shr`
122+
123+
``` purescript
124+
shr :: BigInt -> Number -> BigInt
125+
```
126+
127+
Shift the first number to the left by the second number of bits and return the result.
128+
81129
#### `fromString`
82130

83131
``` purescript

src/Data/BigInt.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,38 @@ exports.pow = function(x) {
9090
return x.pow(y);
9191
};
9292
};
93+
94+
exports.not = function(x) {
95+
return x.not();
96+
};
97+
98+
exports.or = function(x) {
99+
return function(y) {
100+
return x.or(y);
101+
};
102+
};
103+
104+
exports.xor = function(x) {
105+
return function(y) {
106+
return x.xor(y);
107+
};
108+
};
109+
110+
exports.and = function(x) {
111+
return function(y) {
112+
return x.and(y);
113+
};
114+
};
115+
116+
exports.shl = function(x) {
117+
return function(n) {
118+
return x.shiftLeft(n);
119+
};
120+
};
121+
122+
exports.shr = function(x) {
123+
return function(n) {
124+
return x.shiftRight(n);
125+
};
126+
};
127+

src/Data/BigInt.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ module Data.BigInt
1010
, odd
1111
, prime
1212
, pow
13+
, not
14+
, or
15+
, xor
16+
, and
17+
, shl
18+
, shr
1319
, toNumber
1420
) where
1521

@@ -50,6 +56,24 @@ foreign import odd :: BigInt -> Boolean
5056
-- | Returns `true` if the number is prime, `false` otherwise.
5157
foreign import prime :: BigInt -> Boolean
5258

59+
-- | Invert the bits.
60+
foreign import not :: BigInt -> BigInt
61+
62+
-- | or the bits.
63+
foreign import or :: BigInt -> BigInt -> BigInt
64+
65+
-- | Exlusive or the bits.
66+
foreign import xor :: BigInt -> BigInt -> BigInt
67+
68+
-- | and the bits.
69+
foreign import and :: BigInt -> BigInt -> BigInt
70+
71+
-- | shift the bits left and zero fill.
72+
foreign import shl :: BigInt -> Number -> BigInt
73+
74+
-- | Shift the bits right and maintain pos/neg.
75+
foreign import shr :: BigInt -> Number -> BigInt
76+
5377
-- | Parse a string into a `BigInt`, assuming a decimal representation. Returns
5478
-- | `Nothing` if the parse fails.
5579
-- |

test/Main.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Control.Monad.Eff (Eff)
55
import Control.Monad.Eff.Console (CONSOLE, log)
66
import Data.Array (filter, range)
77
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString,
8-
toNumber, fromBase, toString)
8+
toNumber, fromBase, toString, not, or, xor, and, shl, shr)
99
import Data.Foldable (fold)
1010
import Data.Maybe (Maybe(..), fromMaybe)
1111
import Data.NonEmpty ((:|))
@@ -106,3 +106,13 @@ main = do
106106

107107
log "Absolute value"
108108
quickCheck $ \(TestBigInt x) -> abs x == if x > zero then x else (-x)
109+
110+
log "Logic"
111+
assert $ (not <<< not) one == one
112+
assert $ or one three == three
113+
assert $ xor one three == two
114+
assert $ and one three == one
115+
116+
log "Shifting"
117+
assert $ shl two one == four
118+
assert $ shr two one == one

0 commit comments

Comments
 (0)