[Python] Is there a Numpy method or function to split an array of uint64 into two arrays of...

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    Say I have an array as follows:

    arr = np.asarray([1, 2, 3, 4294967296, 100], dtype=np.uint64)


    I now want two arrays, one array with the lower 32 bits of every element, and one with the upper 32 bits of every element, preferably by using views and minimizing copies, to get something like this:

    upper = np.array([0, 0, 0, 1, 0], dtype=np.uint32)
    lower = np.array([1, 2, 3, 0, 100], dtype=np.uint32)


    I tried the following:

    lower = arr.view() & 0xFFFFFFFF
    upper = np.bitwise_right_shift(arr.view(), 32)


    But this results in a copy for the upper bits due to the bitshift, and both arrays are still of type uint64. Are there further optimizations I can try or am I out of luck and need to eat up the extra copies?

    Continue reading...

Compartilhe esta Página