Tagged: bits

Seq.unfold and creating bit masks

In the course of working on ParsecClone I needed some code that could take in an arbitrary byte array and convert it to a corresponding bit array. The idea is if I have an array of

[|byte 0xFF;byte 0x01|]

Then I should get


I’ve done plenty of bit slingin’ in my day, and the trick is just to apply a sequence of bit masks to each byte and collect all the bit arrays. In other languages this is always a little bit of a pain, but in F# the solution was amazingly elegant


As with anything F#, I like to start with the data

type Bit = 
    | One
    | Zero
    override this.ToString() =
        match this with 
            | One -> "1"
            | Zero -> "0"

Make bit masks

Now lets generate the meat and potatoes of this: a sequence of bit masks

let bitMasks = Seq.unfold (fun bitIndex 
Read more