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