diff --git a/docs/Data/BigInt.md b/docs/Data/BigInt.md index be00905..9c41151 100644 --- a/docs/Data/BigInt.md +++ b/docs/Data/BigInt.md @@ -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 diff --git a/src/Data/BigInt.js b/src/Data/BigInt.js index 4f47ca4..4eaa776 100644 --- a/src/Data/BigInt.js +++ b/src/Data/BigInt.js @@ -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); + }; +}; + diff --git a/src/Data/BigInt.purs b/src/Data/BigInt.purs index 332d8b5..46e4cab 100644 --- a/src/Data/BigInt.purs +++ b/src/Data/BigInt.purs @@ -10,6 +10,12 @@ module Data.BigInt , odd , prime , pow + , not + , or + , xor + , and + , shl + , shr , toNumber ) where @@ -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. -- | diff --git a/test/Main.purs b/test/Main.purs index a612815..5d69a8a 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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 ((:|)) @@ -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