I have a big numpy array (i.e. approx. 2 ** 32 positions) of 64-bit unsigned integers and I want to duplicate a single bit (can vary from 0 to 63) in every integer inside this array.
Example:
- Input array:
array([ 0, 5, 2, 7 ]) - Position (right to left) to duplicate:
0
The input, in binary, is
000 101 010 111
After the operation, I want the bits as
0000 1011 0100 1111
- Result:
array([ 0, 11, 4, 15 ])
As stated, my arrays are huge, so I'd prefer to use the lowest number of temporary / auxiliar arrays.
I tried to find something close to it in Google or even Bit Twiddling Hacks with no luck.
Thanks in advance!
(x << 1) | (x & 1).part1 = x & -(1 << pos); part2 = x & ((1 << (pos+1))-1); return (part1 << 1) | part2;. Select two pieces of the integer overlapping at the duplicated bit, and paste those pieces together without overlapping.