keep last n bits

C
unsigned  mask;
mask = (1 << n) - 1;
unsigned lastNbits = value & mask;
Source

Also in C: