Initialize a variable mask. Mask variable = uint32_t mask = ((1u<<len)-1)<<pos; Lets say len=4, it will make the number ...10000. 1 is subtracted form this making it ...01111. Now 4 bits are set and we need to shift to the starting position hence left shifitng by pos.