0

So I have a working example but I think it can be done much more efficient with bitwise operators.

What I am doing is: if I have two int's say in bits: 0001 and 0010 (so in python, 1 and 10 so yeah.) Now the result should be: 00000110. So it is 'zipping' it, first digit of int1 and then first digit of int2.

Currently I make arrays of [0,0,0,1] & [0,0,1,0] and then combine them in a loop. And as we know python is not very good at processing lists. Now with this example its ok but bigger numbers (and it should do this very often!) I think its to slow.

Any suggestions ?

ovanwijk
  • 159
  • 7
  • And preferably not with numpy – ovanwijk Mar 04 '16 at 12:10
  • That's a well-known transformation sometimes called "interleave", "perfect shuffle" (supposes the inputs are concatenated), and other relevant keywords are Z-order curve and Morton numbers (and tesseral arithmetic, in case you're thinking about de-interleaving, doing math, and then re-interleaving). The existing [answers](http://stackoverflow.com/q/24499881/555045) generalize easily to any constant integer size. – harold Mar 04 '16 at 12:19
  • Thnx for the reply, I am indeed making Z-order curves here, it all works but wondering if python has a more efficient way. – ovanwijk Mar 04 '16 at 12:51
  • 1
    More efficient than what? The top answer there takes a number of bitwise operations logarithmic in the number of bits (if any size bit operation counts as 1). It doesn't get much better, unless you have access to `pdep` or an equivalent instruction (in python? probably not) – harold Mar 04 '16 at 12:57

0 Answers0