I really liked this code snippet and the detailed explanation that you have provided.
I have a requirement to parse a 32-bit register with bit fields and produce a human readable values for each field. For example, I have a register definition like:
LinkStatus: 2 # 2 bits for this field
CardStatus: 3
Reserved: 7
IntrStatus: 15
etc..etc
Given a hex value, I would like to output something like:
LinkStatus = 3
CardStatus = 0
Reserved = 0
IntrStatus = 200 etc...
I tried to use the above method, but I guess the field sizes are in 8 bits..
Wondering if we can use pack/unpack for this or should I use the more classical bit extraction operators (<< and >> )?
Appreciate pointers on this. Thanks.