Skip to content

Commit

Permalink
Merge pull request #2 from Proclivis/master
Browse files Browse the repository at this point in the history
Add logic functions.
  • Loading branch information
sharkdp authored Jul 1, 2017
2 parents ce4614c + e6dcfa1 commit 2f33b6d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
48 changes: 48 additions & 0 deletions docs/Data/BigInt.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,54 @@ prime :: BigInt -> Boolean

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

#### `not`

``` purescript
not :: BigInt -> BigInt
```

Returns a bit inverted number.

#### `or`

``` purescript
or :: BigInt -> BigInt -> BigInt
```

Returns a bit or'ed number.

#### `xor`

``` purescript
xor :: BigInt -> BigInt -> BigInt
```

Returns a bit xor'ed numnber.

#### `and`

``` purescript
and :: BigInt -> BigInt -> BigInt
```

Returns a bit and'ed number.

#### `shl`

``` purescript
shl :: BigInt -> Number -> BigInt
```

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.

#### `shr`

``` purescript
shr :: BigInt -> Number -> BigInt
```

Shift the first number to the left by the second number of bits and return the result.

#### `fromString`

``` purescript
Expand Down
35 changes: 35 additions & 0 deletions src/Data/BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,38 @@ exports.pow = function(x) {
return x.pow(y);
};
};

exports.not = function(x) {
return x.not();
};

exports.or = function(x) {
return function(y) {
return x.or(y);
};
};

exports.xor = function(x) {
return function(y) {
return x.xor(y);
};
};

exports.and = function(x) {
return function(y) {
return x.and(y);
};
};

exports.shl = function(x) {
return function(n) {
return x.shiftLeft(n);
};
};

exports.shr = function(x) {
return function(n) {
return x.shiftRight(n);
};
};

24 changes: 24 additions & 0 deletions src/Data/BigInt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module Data.BigInt
, odd
, prime
, pow
, not
, or
, xor
, and
, shl
, shr
, toNumber
) where

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

-- | Invert the bits.
foreign import not :: BigInt -> BigInt

-- | or the bits.
foreign import or :: BigInt -> BigInt -> BigInt

-- | Exlusive or the bits.
foreign import xor :: BigInt -> BigInt -> BigInt

-- | and the bits.
foreign import and :: BigInt -> BigInt -> BigInt

-- | shift the bits left and zero fill.
foreign import shl :: BigInt -> Number -> BigInt

-- | Shift the bits right and maintain pos/neg.
foreign import shr :: BigInt -> Number -> BigInt

-- | Parse a string into a `BigInt`, assuming a decimal representation. Returns
-- | `Nothing` if the parse fails.
-- |
Expand Down
12 changes: 11 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Array (filter, range)
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString,
toNumber, fromBase, toString)
toNumber, fromBase, toString, not, or, xor, and, shl, shr)
import Data.Foldable (fold)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.NonEmpty ((:|))
Expand Down Expand Up @@ -106,3 +106,13 @@ main = do

log "Absolute value"
quickCheck $ \(TestBigInt x) -> abs x == if x > zero then x else (-x)

log "Logic"
assert $ (not <<< not) one == one
assert $ or one three == three
assert $ xor one three == two
assert $ and one three == one

log "Shifting"
assert $ shl two one == four
assert $ shr two one == one

0 comments on commit 2f33b6d

Please sign in to comment.