proposal/RFC: add bSwap to base in Data.Bits

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Thu May 16 15:24:53 CEST 2013


Dear Vincent,

thanks for working on this proposal.

> -- | Default implementation for 'bSwap'
> --
> -- This implementation is intentionally naive. Instances are expected to provide
> -- an optimized implementation for their size.
> bSwapDefault :: (Bits a, Num a) => a -> a
> bSwapDefault = go 0
>   where
>     go !c 0 = c
>     go c  w = go ((c `unsafeShiftL` 8) .|. (w .&. 0xff)) (w `unsafeShiftR` 8)

This can't be right, since it ignores the bitSize. In fact, from
bSwapDefault 1 = 1 one could conclude that the datatype is 1 byte wide.
bSwapDefault (bSwapDefault 256) = 1 shows that bSwapDefault is not its
own inverse.

(As with bitSize, I don't think that there is a sensible implementation
of bSwap for Integer)

I agree with previous posters that 'bSwap' is a bad name; 'byteSwap'
seems better.

Cheers,

Bertram






More information about the Libraries mailing list