uint32_t extract first bits

C
unsigned  mask;
mask = (1 << X) - 1;
lastXbits = value & mask;
Source

Also in C: