in reply to how to pack bits, flags and masks

You're making this too difficult. The dword is a 32-bit native integer -

perl -e'print 0x00500000,$/ 5242880 $
All you need to test or set bits are the bitwise operators &, |, and ^, and their assignment forms, &=, |=, and ^=.

For example, to test if bit 7 is set in $value,

if ($value & 1<<6) { #do something }
The bit manipulations you show should work fine on your data as it is.

Even if you read your number as a decimal string, it should automatically be converted to an unsigned numeric type by the bitwise operators.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: how to pack bits, flags and masks
by anadem (Scribe) on Nov 15, 2004 at 19:18 UTC
    thanks, it does seem simpler without 'pack' and I can even count the set bits with a for loop to test each in turn.

    I'd been hoping to use this fragment from Programming Perl (ch.29.2.189. unpack):
    "The following efficiently counts the number of set bits in a bitstring:
    $setbits = unpack "%32b*", $selectmask;

    but for me

    $value = 5242880; $value = $value | 0x0011; $setbits = unpack "%32b*", $value; printf "$setbits bits are set in %#.8x\n", $value;
    gives
    25 bits are set in 0x00500011

    so my understanding is still inadequate. If you can bear to explain, it would be much appreciated (though I can use the for loop to count bits instead)

    thanks

      $setbits = unpack "%32b*", $selectmask;
      should be:
      $setbits = unpack "%32b*", $packedmask;
      so:
      $setbits = unpack('%32b*', pack('N', $selectmask));
      does the trick
      print(unpack("%32b*", pack('N', 0x00500011)), $/);  # 4