Improved code to use a mask and different values for NBITS
Improved code to use a mask and different values for NBITS