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

Hello Monks:

I am trying to store both the high and low nibbles from a byte into variables. I am able to store the high nibble by doing:
my $highnibble = ((ord $byte[0]) >> 4)); The variable $highnibble prints the right value.

Now, I cannot get the following to work:
my $lownibble = (((unpack ("H", $byte[0]) << 4) >> 4)); The variable $lownibble prints the wrong value (value of $highnibble).
What am I doing wrong?

Thanks, RV

Replies are listed 'Best First'.
Re: Store high/low nibble into variables
by japhy (Canon) on Jun 16, 2006 at 15:39 UTC
    The high nibble (4 bits, right?) is gotten via $byte >> 4; you got that right. The low nibble is simply $byte & 15 (or in place of 15, the values: 0x0F or 0017 or 0b00001111).

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Store high/low nibble into variables
by sgifford (Prior) on Jun 16, 2006 at 15:36 UTC
Re: Store high/low nibble into variables
by unixhome (Novice) on Jun 16, 2006 at 16:06 UTC

    Guys:

    Ok, the following did the trick:
    my $lownible =(ord $byte[0] & 0x0f);

    Thanks, RV