0 people like it.

Flips the bits (eg. 01000001 becomes 10000010)

better title

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
open System
open FsCheck
open Swensen.Unquote

let byteflip n =
    let rec loop n mask acc =
        match mask > 0x00uy with
        | true ->
            let acc = acc >>> 1
            match n &&& mask with
            | 0x00uy -> loop n (mask >>> 1) acc
            | _      -> loop n (mask >>> 1) (acc ||| 0x80uy)
        | false -> acc
    loop n 0x80uy 0x00uy

test <@ byteflip 0b0001101uy = 0b10110000uy @>
test <@ byteflip 0b0000001uy = 0b10000000uy @>
Check.Quick <| fun n -> n = (byteflip >> byteflip) n
namespace System
namespace FsCheck
namespace Swensen
namespace Swensen.Unquote
val byteflip : n:byte -> byte

Full name: Script.byteflip
val n : byte
val loop : (byte -> byte -> byte -> byte)
val mask : byte
val acc : byte
val test : expr:Quotations.Expr<bool> -> unit

Full name: Swensen.Unquote.Assertions.test
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/jT
Posted:10 years ago
Author:
Tags: algorithms