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; ... }