
Hi all, I am a newcomer to Haskell trying to manipulate data at the bit level. More specifically, I am trying to calculate a seven-bit value and a nine-bit value, concatenate the two values as a Word16, and put the result using putWord16. I know that this sounds weird (because it totally is), but it is required by the standard of a networking protocol that I am implementing. The byte seems to be the atomic unit in Haskell, but I need to go subatomic. Currently, I am storing the two values each as Word16s, bitshifting one of them left by nine (using shift in the Data.Bits module), and then adding the two. Is there a simpler and/or more elegant way of achieving this result? Thanks, Dan Rozycki

Disclaimer: I'm a beginner too.
That's essentially what I've resorted to as well. It would be nice to
see standard functions that pull out specific bits of a word, or join
them together; I've been doing them with shifts and ORs as well. I'd
also be interested in knowing if there's a more respected way of doing
this.
As an aside, the Crypto library on Hackage has an embedded library
called Data.LargeWord, which offers Word64, Word128 and Word256
variants in case you need them (though there is (currently) a bug with
right-shifts on these larger words).
On Thu, May 20, 2010 at 3:36 PM, Daniel Rozycki
Hi all, I am a newcomer to Haskell trying to manipulate data at the bit level. More specifically, I am trying to calculate a seven-bit value and a nine-bit value, concatenate the two values as a Word16, and put the result using putWord16. I know that this sounds weird (because it totally is), but it is required by the standard of a networking protocol that I am implementing. The byte seems to be the atomic unit in Haskell, but I need to go subatomic. Currently, I am storing the two values each as Word16s, bitshifting one of them left by nine (using shift in the Data.Bits module), and then adding the two. Is there a simpler and/or more elegant way of achieving this result? Thanks, Dan Rozycki
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Daniel, Daniel Rozycki wrote:
Currently, I am storing the two values each as Word16s, bitshifting one of them left by nine (using shift in the Data.Bits module), and then adding the two. Is there a simpler and/or more elegant way of achieving this result?
That's about what I would do in C. What's wrong with doing it that way in Haskell? GHC should be smart enough to do pretty well with those kinds of combinations. If you do this sort of thing all the time and there are some combinators that would make it more convenient for you - go ahead and make a library out of them. And upload it to Hackage! Regards. Yitz
participants (3)
-
Daniel Rozycki
-
Ron Leisti
-
Yitzchak Gale