3 ms·
Yes very sure. nthbit will extract the nth bit from an integer type. The bitwise logic is the &&& and <<< in F# you use triples for bitwise. But there is a bug
by fleitz 16y ago
Yes very sure.
nthbit will extract the nth bit from an integer type. The bitwise logic is the &&& and <<< in F# you use triples for bitwise. But there is a bug in there, i forgot to times sz by 8
http://msdn.microsoft.com/en-us/library/dd469495.aspx http://msdn.microsoft.com/en-us/library/dd469495.aspx
bitstream turns a sequence of integer types into a sequence of bits.
So if you wanted to turn a stream into bits you'd do something like the following.
new System.IO.FileStream('foo.txt')
|> Seq.unfold (fun s -> (s , match s.read with
| -1 -> None
| x -> Some(x)))
|> bitstream
I just realized that the bitstream function is unnecessary and you could instead write
let bits (bitContainer:'T) : seq<bool> =
[0..(sizeof<'T>*8)]
|> Seq.ofArray
|> Seq.map (fun nth -> (bitContainer &&& ((1 :> 'T) <<<nth))
And change the above code to:
new System.IO.FileStream('foo.txt')
|> Seq.unfold (fun s -> (s , match s.read with
| -1 -> None
| x -> Some(x)))
|> Seq.map bits
|> Seq.concat
- fleitz 16y agoJust for fun here's the reverse to turn a sequence of booleans into a sequence of integer types (byte,int,long,etc). Note that the length of sequence s must be a multiple of sizeof<'T> times 8 let bitsToBitContainer (s:seq<bool>) : seq<'T> = let sz = sizeof<'T>*8 s |> Seq.scan (fun (i,n) x -> (i++,(n<<<1)|||(x&&&1)) (0,0) |> Seq.filter (fun (i,n) -> i = sz) |> Seq.map (fun (i,n) -> n)
- mahmud 16y agoI am liking this F# thing. Thanks! :-)