in reply to Bit string manipulation made easy with Bit::Manip
While looking at the documentation to bit_set() to make sure my "not really necessary" was true, I had some thoughts:
Instead of calling the argument for the number of bits "$bits", I'd be tempted to document it as "$nbits"; some might want to misinterpret that argument name as a bit-mask instead of a number of bits, despite the text on the next line. (ie, when I first saw that, my mind set the "need more information flag" until I got to the next line. (This will especially help clarify if you end up implementing the next suggestion.)
With longer words, there have been times when I want to address multiple disjoint bits simultaneously, such as when a hardware designer separates bits that would logically be grouped. For example, suppose you had a register where in the cheap version of a part, you set a 4bit offset and an offset-enable, where the enable is in bit 4, and the 4 bit offset is bits[3:0] = offset[3:0]; but in a more expensive but pin-and-register compatible version, the same register has an additional 2 bits of offset above the sign bit, so it's bits[6:5] = offset[5:4], bits[4] = enable, bits[3:0] = offset[3:0]. (I design tests for hardware for a living; I've seen much more convoluted register words than that.) To that end, I would want
my $data = 0x00; bit_on($data, 4); # enables the offset my $offsetBits = bit_mask_disjoint(6,5,3,2,1,0); foreach my $val ( 0..63 ) { bit_set_disjoint($data, $offsetBits, $val); # sets the offset + bits without affecting the enable bit ... } ... my $ctrl = 0x00; # ctrl is a three-bit register my $permuteBits = [1,0,2]; # but HW designer and I think of the bits +as being in a different order foreach my $val ( 0..7 ) { bit_set_disjoint($ctrl, $permuteBits, $val); # 0:000; 1:010; 2: +100; 3:110; 4:001; 5:011; 6:101; 7:111; ... }
(I don't particularly like the _disjoint() naming convention... you might want to see if you can come up with a better one.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bit string manipulation made easy with Bit::Manip
by stevieb (Canon) on Jan 30, 2017 at 22:17 UTC | |
by stevieb (Canon) on Jan 31, 2017 at 22:43 UTC | |
|
Re^2: Bit string manipulation made easy with Bit::Manip
by stevieb (Canon) on Feb 01, 2017 at 20:16 UTC | |
by pryrt (Abbot) on Feb 01, 2017 at 22:55 UTC | |
by stevieb (Canon) on Feb 03, 2017 at 01:08 UTC |