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

Vincent Hanquez tab at
Thu May 16 11:36:46 CEST 2013

On Thu, May 16, 2013 at 05:07:51AM -0400, Edward Kmett wrote:
> Henning has a point.
> EndianSensitive is arguably the more appropriate notion.

Yes, Bits is not necessarily the best fit in term of naming or feature, but
nothing close to EndianSensitive is in base.

> What does it mean to 'byteSwap' an 'Integer'? Or a bit vector of length n?

It would mean the same as byteswapping a Word32/Word64, the 1st byte would
be at the end, the second byte ...

+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)

It doesn't necessarily make complete sense to byteswap arbitrary Bits types
(either for performance reason like integer, or for a non-8-bytes multiple), however
it's possible to come up with a definition that somewhat make sense
generically for any bits types.


More information about the Libraries mailing list