1

Consider 3 integers of 2 bits, stored in an array

x1 = [ 
    ad,
    be,
    cf,
]

The integers are shown above with their bits, but they are stored as regular int.
The array x1 is the transpose of the integer x = abcdef.

How to untranspose x1 to get

x2 = [ 
    ab,
    cd,
    ef,
]

The array x2 is simply x in natural order.

What is an efficient way to do this in python?

My ultimate goal is to do comparison between integers.
So given integers a and b stored like x1, I want to know if a > b, by looking at a1 and b1.
Any way to achieve this goal, is a good answer as well.

I can use a string representation of integers, but I would like to use some bit shift magic.

This question is a converse to Efficient way to transpose the bit of an integer in python?

Community
  • 1
  • 1
  • 2
    Your question isn't clear -- do you have a Python lists of length 3, which consists of 3 Python lists of length 2, and you want to shuffle those elements as shown, or do you have three integers in the range 0 to 3, which you are thinking of as corresponding to a matrix, and you want to get another collection of 3 (or is it 1?) integer from that. Please clarify the form of the input and the form of the desired output. – John Coleman Jun 16 '16 at 15:50
  • @JohnColeman Thanks. –  Jun 16 '16 at 15:56
  • It sounds like an interesting problem, but I don't want to spend much time thinking about it before I know better what the problem actually is. – John Coleman Jun 16 '16 at 15:59

1 Answers1

2

Here is one way:

def untranspose(arr):
    x,y,z = arr
    a,d = divmod(x,2)
    b,e = divmod(y,2)
    c,f = divmod(z,2)
    return f + 2*e + 4*d + 8*c + 16*b + 32*a

For example,

>>> untranspose([1,3,2])
30

[1,3,2] corresponds to

01
11
10

so you want the integer 011110:

>>> int('011110',2)
30
John Coleman
  • 51,337
  • 7
  • 54
  • 119