This function copies bits from a starting point (bitFrom) upto a given length of variable mask and sets the bit in the corresponding position in variable V.

void copyBits(unsigned int& V, int bitFrom, int length, unsigned int mask){

int m1 = 1 << (bitFrom – 1);
int m2 = m1 & mask;

for (int i=bitFrom+1; i<=bitFrom+length; i++) {

int m1 = 1 << (i-1);
bool mbit = !!(mask&m1); //Gets ith bit of mask

setBit(V, i, mbit); //Sets ith bit of V to the ith bit of mask
}
}