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

Community
  • 1
  • 1
  • 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? – Patrick Maupin Aug 21 '15 at 02: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 –  Aug 21 '15 at 06: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... – Patrick Maupin Aug 21 '15 at 15:05
  • @ Patrick Maupin fields order is LSB is first to MSB, AAAA is at the MSB of number. expecting your valuable response –  Aug 23 '15 at 11:30
  • Can you give us an example of what the values of the BitFields should be for your input? – DoctorSelar Aug 23 '15 at 13:05
  • Your are passing 128bits not 64 – Padraic Cunningham Aug 23 '15 at 14:48
  • Now its corrected, mistake from my side. Pls give your valuable inputs –  Aug 23 '15 at 14:54
  • It still doesn't make sense -- if I have an initialization of `'0b10111010'`, that should be the same as `'0xBA'`, not `'0xAB'`. But your initialization string is the other way around. – Patrick Maupin Aug 23 '15 at 15:23
  • could you please explain bit more, my intension is Bitfield 1- LSB and Bitfield7 is MSB –  Aug 23 '15 at 15:27

1 Answers1

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')
DoctorSelar
  • 486
  • 2
  • 7
  • 16
  • 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 –  Aug 23 '15 at 14:47
  • 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. – DoctorSelar Aug 23 '15 at 15:04
  • "You can assign the variables to the values from the "self.fields" variable as needed", could you please elaborate, any code snippet highly appreciated –  Aug 23 '15 at 15:19
  • 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 –  Aug 23 '15 at 15:51
  • How would those fields match up with the Bitfields you specified? (i.e. is self.MO Bitfield1 or Bitfield7) – DoctorSelar Aug 23 '15 at 16:03
  • self.MO is Bitfield7 and self.RES is Bitfield7 .@DoctorSelar I have to assign is it? –  Aug 23 '15 at 16:36
  • So they're both Bitfield7? I'm a bit confused with how the Bitfields match up to the variables you specified. – DoctorSelar Aug 23 '15 at 19:19
  • self.MO is Bitfield7 and self.RES is Bitfield0 .@DoctorSelar , Its typo mistake. self.RES = x & ((2 * * 6)-1) x = x >> 6 self.DT = x & ((2 * * 12)-1) x = x >> 12 self.CLK = x & ((2 * * 6)-1) x = x >> 6 self.SO = x & ((2 * * 4)-1) x = x >> 4 self.MI = x & ((2 * * 12)-1) x = x >> 12 self.SI = x & ((2 * * 8)-1) x = x >> 8 self.MO = x & ((2 * * 16)-1) , M i going in in the right direction? Please coorect me , if m wrong. –  Aug 23 '15 at 19:45
  • It's difficult to read without formatting, why don't you try it on some test data? – DoctorSelar Aug 23 '15 at 20:54