1

I want to read bits in 64 bit data put into corresponding bitfields of the Register class, I don't want to use bit array module, is there any traditional approach in python to achieve this.

I researched on this topic i got the following link -- [1]: How to read bits from a file? [bit field read link][1] but it doesn't help, Example code snippet is highly appreciated.Thanks in advance.

class Register(object):
    def __init__(self, x): 

       self.BitField7= 0
       self.BitField6= 0
       self.BitField5= 0
       self.BitField4= 0
       self.BitField3= 0
       self.BitField2= 0
       self.BitField1= 0

       self.fieldwidths = (6,12,6,4,12,8,16)
       self.field_names=["BitField1","BitField2","BitField3","BitField4","BitField5","BitField6","BitField7"]

obj= Register('0b11011101110111011101110111011101110011001100110011001100110011001011101110111011101110111011101110101010101010101010101010101010') # input is 0xAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD

print obj # should print 0xAAAAAAAABBBBBBBB

Thanks in advance

9
  • Normally you would define a generic register class, and then either subclasses or different values in instances for each register. This appears to be hard-coded to a particular set of values. What exactly are you trying to accomplish? Commented Aug 21, 2015 at 2:22
  • @Patrick Maupin I want based on the input I want to slice input to since I already know feildwidths eg: input/self.fieldwidths = (6,12,6,4,12,8,16), after slicing this I want to put this into corresponding bitfields Commented Aug 21, 2015 at 6:12
  • Are your fields MSB first or LSB first? And why do you expect that to print in that order? AAAA is at the bottom of your number... Commented Aug 21, 2015 at 15:05
  • @ Patrick Maupin fields order is LSB is first to MSB, AAAA is at the MSB of number. expecting your valuable response Commented Aug 23, 2015 at 11:30
  • Can you give us an example of what the values of the BitFields should be for your input? Commented Aug 23, 2015 at 13:05

1 Answer 1

1

The following code will load the requested portions of the binary number into the fields:

class Register(object):
    def __init__(self,x):
        self.fieldwidths = [6,12,6,4,12,8,16]

        ## Reverse the input string then convert it to an integer
        x = int(x[2:][::-1],2)   

        ## To keep code size down, store fields in a list (+ laziness)
        ## [BitField1, BitField2, ...,BitField7]
        self.fields = [0,0,0,0,0,0,0]

        for i,width in enumerate(self.fieldwidths):
            ## You can change the variables this stores the values in to
            ## your liking, but this is the basic procedure

            ## Store the last "width" number of bits in the current field
            ## e.g. 0b1000101 & 0b111 (or (2**3)-1) yields 0b101
            self.fields[i] = x & ((2**width)-1)

            ## Chop off the last "width" number of bits from x
            ## e.g. 0b1010 >> 1 would result in 0b101
            x = x >> width

obj = Register('0b11011101110111011101110111011101110011001100110011001100110011001011101110111011101110111011101110101010101010101010101010101010')

Which will result in:

BitField1 = 0b111011
BitField2 = 0b111011101110
BitField3 = 0b101110
BitField4 = 0b1011
BitField5 = 0b1100111011
BitField6 = 0b110011
BitField7 = 0b11001100110011

The results may not be what you wanted because of the fact that the data you provided is not 64 bits but rather 128 bits, which would mean that the 64 most significant bits of the input data will be ignored by the program.

EDIT:

If you wanted to just hardcode variable names and fieldwidths, you could just expand the for loop and assign the variables:

class Register(object):
    def __init__(self,x):
        ## Reverse the input string then convert it to an integer
        x = int(x[2:][::-1],2)

        self.Bitfield1 = x & ((2**6)-1)
        x = x >> 6

        self.Bitfield2 = x & ((2**12)-1)
        x = x >> 12

        self.Bitfield3 = x & ((2**6)-1)
        x = x >> 6

        self.Bitfield4 = x & ((2**4)-1)
        x = x >> 4

        self.Bitfield5 = x & ((2**12)-1)
        x = x >> 12

        self.Bitfield6 = x & ((2**8)-1)
        x = x >> 8

        self.Bitfield7 = x & ((2**16)-1)

obj = Register('0b11011101110111011101110111011101110011001100110011001100110011001011101110111011101110111011101110101010101010101010101010101010')
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks a lot!! actual fieldwidth names are self.BitField7= 0 self.MO = 0 self.SI = 0 self.MI = 0 self.SO = 0 self.CLK = 0 self.DT = 0 self.RES = 0 to simplify I have given these names BitField0..7 could you pls let me know how can achieve with above given bitfield names
You can assign the variables to the values from the "self.fields" variable as needed, though I'm not sure how you would assign the 7 values you provided field widths for to the 8 variables you want.
"You can assign the variables to the values from the "self.fields" variable as needed", could you please elaborate, any code snippet highly appreciated
Could you pls tell me, how could I achieve with these inputs --- self.MO = 0 self.SI = 0 self.MI = 0 self.SO = 0 self.CLK = 0 self.DT = 0 self.RES = 0
How would those fields match up with the Bitfields you specified? (i.e. is self.MO Bitfield1 or Bitfield7)
|

Your Answer

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