user2768702
user2768702

Reputation:

How read bits in 64 bit data put into corresponding bitfields in python

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

Upvotes: 1

Views: 516

Answers (1)

DoctorSelar
DoctorSelar

Reputation: 484

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')

Upvotes: 1

Related Questions