You need this:
uint64_t mask = (1uLL << (len * 4)) - 1;
data_tmp = data_tmp & mask;
This will give you the lowest len
of 4-bit digits. If you need highest instead, you can rotate data_tmp
before applying the mask:
data_tmp >>= (64 - len * 4)