utility functions

This commit is contained in:
Stephen Diehl
2016-12-10 16:01:47 +00:00
parent 00c2fce11a
commit 6cb17bb94b
8 changed files with 159 additions and 2 deletions
+55
View File
@@ -1,6 +1,14 @@
Bits
====
```haskell
{-# LANGUAGE BinaryLiterals #-}
```
```python
42 = 0b101010
```
Bit Operations
--------------
@@ -28,12 +36,31 @@ xor :: Bits a => a -> a -> a
*Example*:
```haskell
> True `xor` False
True
> True `xor` True
False
> 0b101 `xor` 0b011
6 -- 0b110
```
#### complement
```haskell
complement :: Bits a => a -> a
```
```haskell
> complement True
False
> complement 0b101
-2 -- -0b010
```
*Example*:
#### shift
@@ -81,19 +108,47 @@ setBit :: Bits a => a -> Int -> a
clearBit :: Bits a => a -> Int -> a
```
#### complementBit
```haskell
complementBit :: Bits a => a -> Int -> a
```
*Example*:
```haskell
λ> 0b100 `complementBit` 1
6 -- 0b110
```
#### testBit
```haskell
testBit :: Bits a => a -> Int -> Bool
```
*Example*:
```haskell
> 0b10 `testBit` 0
False
> 0b10 `testBit` 1
True
```
#### isSigned
```haskell
isSigned :: Bits a => a -> Bool
```
```haskell
> isSigned 42
True
> isSigned True
False
```
Bit Size
------------