3

I am trying to do 128 bit manipulation in Python, to split the 128 bit after 32 bit

var = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF # var may take any value from 0x0 to  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

I have tried the

n = 8 # for 32 bit splitting
[var[i:i+n] for i in range(0, len(var), n)]

But there is no built-in length operator to find the length for long variable. I researched bitarray module, I don't want to use bit array in my code, is there any thing in pythonic way?

Expected output would be [0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 0xFFFFFFFF].

4
  • Do you mean [0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF]? And are you declaring var with quotes? Commented Aug 17, 2015 at 14:39
  • No, I just edited my question please look into it Commented Aug 17, 2015 at 14:41
  • Can you use python3 instead of python2? Commented Aug 17, 2015 at 15:05
  • Trengo Thanks !! Good to know, I will switch to python 3 Commented Aug 17, 2015 at 15:08

2 Answers 2

5

You could use bit shifting (>>) and bitwise AND (&):

In [10]: [(i >> x) & 0xFFFFFFFF for x in reversed(range(0, 128, 32))]
Out[10]: [4294967295, 4294967295, 4294967295, 4294967295]

where i is your integer. The resulting list will have 4 elements (because 128 / 32 is 4).

In your case the result is 4 equal numbers (4294967295, because 4294967295 is 0xFFFFFFFF in hex).

This works well for a known number of bits (32), but I'd rather use a helper function (following the same logic as above):

def split_bits(value, n):
    ''' Split `value` into a list of `n`-bit integers '''
    mask, parts = (1 << n) - 1, []
    parts = []
    while value:
        parts.append(value & mask)
        value >>= n
    parts.reverse()
    return parts

In action:

In [14]: split_bits(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 32)
Out[14]: [4294967295, 4294967295, 4294967295, 4294967295]

In [15]: [format(i, 'X') for i in _] # show corresponding hex representations
Out[15]: ['FFFFFFFF', 'FFFFFFFF', 'FFFFFFFF', 'FFFFFFFF']

In [16]: split_bits(0b1000100110101011, 4)
Out[16]: [8, 9, 10, 11]

In [17]: [format(i, 'b') for i in _] # show corresponding binary representations
Out[17]: ['1000', '1001', '1010', '1011']
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot. is it possible to find the length of long Interger? I may need it for future, is there any possible way?
@Jonny yes, see int.bit_length.
I have already tried it, but I am puzzled its throwing this error TypeError: descriptor 'bit_length' requires a 'int' object but received a 'long'.
@Jonny hmm, what's the version of your Python interpreter?
Just throwing this out there, but splitting a negative value in this way produces an endless loop. split_bits(-1) Due to the way that Python represents numbers.
|
0

it's better to work with numbers instead of stirng

var = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[(var & (0xFFFFFFFF << 32*i)) >> (32*i) for i in range(4)]

eventually you can conver number to string hex representation

format(n, 'x')

1 Comment

Thankew so much farincz. I have edited my problem statement please look into it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.