2

I want to use Data.Bits bitwise operators on a binary data. What data type I need to use? I wrote this function for example :

ch :: Int8 -> Int8 -> Int8 -> Int8
ch x y z = (x .&. y) `xor` ((complement x) .|. z)

I want to get as input binary type or at least String (not Int) but still use Data.Bits

So what I need to change to use it like this for example? : ch 11111111 101110011 10110101

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
limitless
  • 669
  • 7
  • 18
  • Binary data? Like a stream of `Word8`? A `ByteString`? – Zeta Jan 02 '18 at 17:06
  • Well im not sure. Im tring to implement "sha256" function so Im getting `String` of text as input and immediately conveting it to the representing binary text. so now it is `String`. – limitless Jan 02 '18 at 17:15

2 Answers2

4

As written, ch will only take Int8 values, but you can relax the type of the function without changing its implementation:

import Data.Bits

ch :: Bits a => a -> a -> a -> a
ch x y z = (x .&. y) `xor` (complement x .|. z)

Binary values are just particular representations of integers, but to make things more readable, you can enable BinaryLiterals, e.g. like this in GHCi:

*Q48065001> :set -XBinaryLiterals
*Q48065001> ch 0b11111111 0b101110011 0b10110101
-58

In a code file, you can enable that with a language pragma:

{-# LANGUAGE BinaryLiterals #-}

If you have string representation of binary numbers, you'll first need to parse those. See e.g. Convert a string representing a binary number to a base 10 string haskell

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Thank you. one small thing, giving string `"11111111"` representation how do you convert to `0b11111111` `BinaryLiterals` type? – limitless Jan 02 '18 at 17:27
  • But he is converting to the representing integer. I think I missed how to change it to convert to the `0bXXX` type we talked about. – limitless Jan 02 '18 at 18:12
  • 1
    @limitless An expression like `0b101010` isn't a data type; it's a representation. It's just a way to write the number `42`. If you go `0b101010 :: Int8` it's `42` as an `Int8`. If you go `0b101010 :: Word64`, it's `42` as a `Word64`. You can also represent `42` a hexadecimal number, like this: `0x2a :: Word64`. It's still `42` as a `Word64`; it's just two different ways of writing it. – Mark Seemann Jan 02 '18 at 18:42
0

A good choice for I/O on binary data is Data.ByteString.Lazy, a highly-optimized library for reading Word8 values. It includes analogues for most of the list and I/O functions in the Prelude and in Data.List, and is well-suited for efficient interactive I/O because it reads bytes in and can write them out as a lazily-evaluated list of strict arrays. It also has efficient functions to convert to and from various encodings. See also this tutorial.

Davislor
  • 14,674
  • 2
  • 34
  • 49