in reply to Bit string manipulation made easy with Bit::Manip

I've been using Bit::Vector for similar bit groupings within a 16-bit value when doing TCP/IP header manipulation with Net::Frame, its sub-modules and Perl Packet Crafter.

I always found Bit::Vector a bit hard to understand, but it did what I needed. Your approach seems a bit more flexible and friendly.

  • Comment on Re: Bit string manipulation made easy with Bit::Manip

Replies are listed 'Best First'.
Re^2: Bit string manipulation made easy with Bit::Manip
by stevieb (Canon) on Jan 25, 2017 at 18:52 UTC

    Thanks!

    I honestly wrote this just for practice, not because I needed software to do the bit mangling for me (as I'm getting quite good at both implementing and reading it), so I honestly didn't even look whether there were other modules that do this sort of thing before I started.

    I just thought that it may help others (or even myself if this stuff falls out of my brain in the future) :)

    ps. There's a bug in bit_set() that I've had to fix in the next version, which requires it to accept an extra parameter, so any code written against 0.02 will break if the next release comes out).

      Mentioned bug is fixed in Bit::Manip v1.01 and Bit::Manip::PP v1.00. Essentially, I had to add an additional param to bit_set():

      ... my $lsb = 2; my $num_bits_to_change = 3; my $value = 0b001; $data = bit_set($data, $lsb, $num_bits_to_change, $value); ...

      What was happening is that the leading zeroes in $value (0b001)) were not being counted as the length of the number of bytes to modify. The code strips all leading zeroes, so the number of bits would turn out to be one, whereby we need to change three bits. The first two (MSBs) get updated to be 0, and the last (the LSB specified) goes to 1.

      _