How can I optimise this bitmask generation algorithm? I am emulating the left shifting of a 128-bit integer using two 64-bit integers. For this I must calculate the bits that need to be moved into the higher portion. I have the following algorithm for generating a bitmask that masks the last until bits.

Where until is an integer in the range 0..64:

 ~(power(2,64-until))-1)


For convenience this is it in Python:

def mask(until): bin((~(2**(64-until)-1))&amp;0xFFFFFFFFFFFFFFFF)


Is there a faster way to do the same thing by avoiding the costly use of power()? Or is this the most efficient way?
You can compute a power of 2 using the operation of left shift, in C and related languages &lt;&lt;. For example, $2^n$ is the same as 1 &lt;&lt; n.

An alternative is to precompute the mask for all possible input values, and store them in the array. You'll have to try it out to see whether this is faster than the other suggestion.