haskell - Creating a bitfield with Data.Bits -
i want use bitfield 32 bits store result of last 32 computations in sequence. e.g. 0110 ... fail pass pass fail ... . ideally latest result should pushed on bitfield whilst oldest result "falls away" on other side. example: 0110 1 newest result should become 1101.
i struggling use of data.bits
, don't know how initialise / create bitfield. errors similar
no instance (bits a0) arising use of `bitdefault' type variable `a0' ambiguous possible fix: add type signature fixes these type variable(s) note: there several potential instances: instance bits int -- defined in `data.bits' instance bits integer -- defined in `data.bits' instance bits ghc.types.word -- defined in `data.bits'
once add type signatures ghc tells me
illegal literal in type (use -xdatakinds enable): 32
or similar errors. requests kind *
etc.. think basic missing here. how can create bitfield?
just use word32
data.word
bitmap. type has bits
instance , can use methods of typeclass bits
on it:
import data.bits import data.word type bitmap32 = word32 bitmap :: word32 bitmap = 0 -- initialize zeros ... testbit bitmap 10 -- test 10th bit ... pushnewbit :: bool -> bitmap32 -> bitmap32 pushnewbit b bitmap = (bitmap `shiftl` 1) .|. (if b 1 else 0)
Comments
Post a Comment