bladx has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have been studying lately on the topic of most signficant bit first order, and I was simply wondering what all of the different bits there are. The book I am reading doesn't seem to go into detail that I know of, in how many bits are in a byte. If I remember correctly with my old C programming class, there are something of 8 bits in 1 byte. Is that right?

My second real question is: How does Perl decide which bit is more significant?

Please help unconfuse me :).

Andy

Replies are listed 'Best First'.
Re: The skinny on MSB.
by arturo (Vicar) on Aug 09, 2001 at 22:45 UTC

    The perl binary "knows" because it is built for a particular system. Any bit-fiddling that needs doing internally is automatically handled, by routines that are built based on the system architecture.

    Of course, that doesn't mean that any particular application that one might write to fiddle with bits automatically knows. You have to figure out what kind of system you're fiddling bits for to figure out whether it's "big endian" or "little endian"; how you figure that out depends on your application.

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: The skinny on MSB.
by petral (Curate) on Aug 09, 2001 at 23:49 UTC
    Just a little bit of explicit unconfusion. The bit order always appears to be high bit first inside the cpu. That is, "left-shift" always doubles the value and "right-shift" always halves it which implies that the high bit is on the "left".  Bit-order can matter in serial transmissions, but only at very low levels.
    It is byte order which is visible to application software and that's what the above answers are about (as, indeed, they say).  Sometimes, books which deal with both (eg, books on networking protocols), will use "msb" for most significant bit and "MSB" for most significant byte.

      p

Re: The skinny on MSB.
by nakor (Novice) on Aug 09, 2001 at 22:50 UTC
    8 bits per byte is standard now (if you use a PDP-8 or something, things may get more interesting...). On your second question, Configure (the Perl configuration detector script - it gets run before your perl is compiled) has a test for _byte_ order. It doesn't matter to the perl programmer, since standard perl numbers are in whatever byteorder is native to the platform. If you really care, pack is your friend (in particular the formats N, n, V, and v).