0 people like it.

Byte to bit list

Converts a byte list to a bit list. Takes a list of bytes and transforms it all into a flattened array of bits. For example, > bytesToBits [|byte(0x0F);byte(0xF0)|] 16 ;; val it : byte [] = [|0uy; 0uy; 0uy; 0uy; 1uy; 1uy; 1uy; 1uy; 1uy; 1uy; 1uy; 1uy; 0uy; 0uy; 0uy; 0uy|]

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
let bytesToBits (bytes:byte[]) bitCount =
    let numBytesToFetch = (bitCount - 1) / 8
    let bytes = bytes.[0..numBytesToFetch]
    let bitMasks = Seq.unfold (fun bitIndex -> Some((byte(pown 2 bitIndex), bitIndex), bitIndex + 1)) 0     
                        |> Seq.take 8
                        |> Seq.toList
                        |> List.rev

    let byteToBitArray b = List.map (fun (bitMask, bitPosition) -> (b &&& bitMask) >>> bitPosition) bitMasks

    bytes
        |> Array.toList
        |> List.map byteToBitArray
        |> List.collect id
        |> List.toArray
        |> fun i -> i.[0..bitCount - 1]
val bytesToBits : bytes:byte [] -> bitCount:int -> byte []

Full name: Script.bytesToBits
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
val bitCount : int
val numBytesToFetch : int
val bitMasks : (byte * int) list
module Seq

from Microsoft.FSharp.Collections
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
val bitIndex : int
union case Option.Some: Value: 'T -> Option<'T>
val pown : x:'T -> n:int -> 'T (requires member get_One and member ( * ) and member ( / ))

Full name: Microsoft.FSharp.Core.Operators.pown
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val byteToBitArray : (byte -> byte list)
val b : byte
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val bitMask : byte
val bitPosition : int32
module Array

from Microsoft.FSharp.Collections
val toList : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.Array.toList
val collect : mapping:('T -> 'U list) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.collect
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
val toArray : list:'T list -> 'T []

Full name: Microsoft.FSharp.Collections.List.toArray
val i : byte []

More information

Link:http://fssnip.net/jB
Posted:11 years ago
Author:devshorts
Tags: low level , byte