in reply to Factoring out common code in two methods of a class

further reading of the docs tells me that pack/unpack is not the way to do the bit-level twiddling. So I am looking into vec for the flag handling.

Look at vec by all means, but don't be surprised if you come back to unpack, or switch to masking and shifting.

The problem with vec is that it will only deal with powers-of-two numbers of bits. For example, there is no way to get at your 7-bit field directly using vec.

Of course, unpack has its problems too. Getting a 7-bit field back as 7 individual 0|1s isn't ideal. And you can get unintuative results if you attempt to unpack bits across byte-boundaries also.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW
  • Comment on Re: Factoring out common code in two methods of a class

Replies are listed 'Best First'.
Re^2: Factoring out common code in two methods of a class
by jvector (Friar) on Aug 06, 2009 at 10:07 UTC
    The problem with vec is that it will only deal with powers-of-two numbers of bits. For example, there is no way to get at your 7-bit field directly using vec.
    Aiee! I had found the existence of vec and had it in a tab on my browser but had not studied it at the time I wrote that update...

    I had also done a CPAN search ( for "bits", as I recall) and there was one bit-manipulating utils module there (I don't recall the exact name) that had the same restriction (only operable on fields of 1,2,4,8... bits). Since these data structures tend to be dreamed up by hardware/comms engineers who are concerned with cramming the maximum amount of "useful" information into the smallest amount of bits, that restriction is not very helpful.

    Getting a 7-bit field back as 7 individual 0|1s isn't ideal.
    But it may be the least ugly/painful way. There's obviously MTOW to do it but not all are equally attractive.

    The last signature was better than this one