Extract bit fields from int in Python

I have a number like 0x5423where I want to extract 4 values:

a = 0x5   # 15 downto 12
b = 0x42  # 11 downto 3
c = 0x3   # 3 downto 2
d = 0x00  # 1 downto 0

I found a bitstron module that looks great. Unfortunately, for some unknown reason, the bits are numbered on the right.

This is bad because if you add some high bits, such as 0xA5423, my extraction will no longer work:

field = bitstrings.BitArray('0x5423')
a = field[0:4].uint
b = field[4:12].uint
c = field[12:14].uint
d = field[14:16].uint

How can I correctly extract my bit fields without complicated arithmetic manipulations such as:

b = (a >> 4) & 0xFF

Ideally, I would:

b = field.range(11, 4)
+4
source share
1 answer

Convert the string to a format 0x####before proceeding to bitstring.BitArray:

>>> n = '0xA5423'
>>> n = '0x{:04x}'.format(int(n, 16) & 0xffff)  # => '0x5423'
>>> field = bitstring.BitArray(n)
>>> field[0:4].uint
5
>>> field[4:12].uint  # 0x42 == 66
66
>>> field[12:14].uint
0
>>> field[14:16].uint
3

UPDATE , bitstring, ( OP):

:

>>> n = '0xA5423'
>>> n = format(int(n, 16), '016b')[::-1]  # reversed
>>> n
'11000100001010100101'
>>> int(n[0:2][::-1], 2)  # need to reverse again to get proper value
3
>>> int(n[2:4][::-1], 2)
0
>>> int(n[4:12][::-1], 2)
66
>>> int(n[12:16][::-1], 2)
5
+1

Source: https://habr.com/ru/post/1619164/


All Articles