Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Ascending vs descending bit order

by dcpve (Sexton)
on Jul 01, 2002 at 14:55 UTC ( #178576=perlquestion: print w/replies, xml ) Need Help??
dcpve has asked for the wisdom of the Perl Monks concerning the following question:

In the documentation for pack, the two options for dealing with bit strings are b and B. These two differ in that the former uses ascending bit order and the latter uses descending bit order.

What is the difference between these two? Why would I use one over the other?

Replies are listed 'Best First'.
Re: Ascending vs descending bit order
by Abigail-II (Bishop) on Jul 01, 2002 at 15:16 UTC
    A typical use for pack and unpack is when you have to read (binary) stuff that was written by some other program (for instance, a C program), or you need to write stuff that's going to be read by another program. That other program might not be on the same machine/architecture. So, if you need to write some bit string for another program, and that program expects bits in a byte to be ascending, you'd use 'b'; if that program expects bits in a byte to be descending, you'd use 'B'.

    Of course, if you control both the reading and the writing program, use whatever you feel like.


Re: Ascending vs descending bit order
by Aristotle (Chancellor) on Jul 01, 2002 at 15:44 UTC
    If you control both the writing as well as the reading of this data, while it doesn't make any difference it would probably be a good idea to use B - likely the bit order a human would expect upon first sight when looking at the data.

    Makeshifts last the longest.

(tye)Re: Ascending vs descending bit order
by tye (Sage) on Jul 02, 2002 at 14:19 UTC

    The difference is that "b" is broken because it expects/produces the bits in the wrong order ["001" becomes chr(4)] while "B" is broken because short bit counts only deal with the highest bits ["001" becomes chr(32)]. (: [If multiple adjacent "b"/"B" formats could extract/set successive bits within a single byte, then this combination would be more reasonable.]

    So, if you are a normal person, you have to use "B" and pad your binary strings (in front, that is, to the left) to multiples of 8 characters (for pack) and only use multiples of 8 as lengths after the "B" (for unpack).

    It is called "ascending" bit order because it is assumed that you are reading the base-2 ASCII string (often confusingly called "binary" despite that term having way too many meanings in the context of pack and unpack) from left to right and, if you do that, the bits ascend in value from bit0=1, bit1=2, bit2=4, etc. While with "descending" bit order the leftmost "0" or "1" represents bit8=128 and the bit values descend from there.

    BTW, I find that the documentation for pack/unpack is rather ambiguous/confusing and it is very hard to make it completely unambiguous. I often resort to testing via "perl -de 0" when I have a question about some detail of these two functions. You might find that useful as well.

            - tye (but my friends call me "Tye")
Re: Ascending vs descending bit order
by smitz (Chaplain) on Jul 01, 2002 at 15:24 UTC
    Big Endian and Little Endian for the difference.
    Why one is used over another is mostly (/me thinks ?) down to the system.

      Careful, nothing to do with big vs little endian which are byte order variations, as opposed to the pack formats which differ in the bit order. Observe:
      $ perl -le'print ord pack "B*", "00000001"' 1 $ perl -le'print ord pack "b*", "00000001"' 128

      Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178576]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-01-20 18:27 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (176 votes). Check out past polls.