getting at individual bits inside byte field: struct module : bitwiseoperator

Discussion in 'Python' started by harijay, Feb 24, 2009.

  1. harijay

    harijay Guest

    In my last post I had asked about reading data from a binary file
    using the struct module.
    Thanks to some excellent help , I have managed to read in
    successfully
    most of the header of this binary format that I want to parse. These
    are some time-voltage traces from a digital
    to analog converter for my experiments. The output is according to a
    format
    mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)

    I have a question about how to bitmask a bunch of bytes read in from
    such a binary formatted file .

    For eg the spec says the first two bytes have different parameters in
    different bits .
    Byte 1 Byte 0
    SN16 SD9 SD8 SD7 SD6 SD5 SD4 SD3
    SD2 SD1 SD0 T4 T3 T2 T1 T0

    I am reading in the two bytes using the following code

    import struct
    f.seek(0)
    element1_format = struct.Struct("<H")
    (element1,) = element1_format.unpack(f.read(2))

    Now element1 has type "str" . How do I apply a bitmask to this to get
    at information in the component bits .
    Since the entire file format has many such bitmasked fields and since
    this is my first venture into binary formats and c-type structs , I
    wanted to know how to read values inside a byte using python.
    My few tries at using bitwise operators ( element1 & 0x001f) are
    frustrated by messages that say " unsupported operand type(s) for &:
    'str' and 'int' " .
    How can I keep my string objects as bits and apply bitmasks to them
    Any help in this will be greatly appreciated.
    Thanks
    hari
     
    harijay, Feb 24, 2009
    #1
    1. Advertising

  2. harijay

    MRAB Guest

    Re: getting at individual bits inside byte field: struct module :bitwise operator

    harijay wrote:
    > In my last post I had asked about reading data from a binary file
    > using the struct module.
    > Thanks to some excellent help , I have managed to read in
    > successfully
    > most of the header of this binary format that I want to parse. These
    > are some time-voltage traces from a digital
    > to analog converter for my experiments. The output is according to a
    > format
    > mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)
    >
    > I have a question about how to bitmask a bunch of bytes read in from
    > such a binary formatted file .
    >
    > For eg the spec says the first two bytes have different parameters in
    > different bits .
    > Byte 1 Byte 0
    > SN16 SD9 SD8 SD7 SD6 SD5 SD4 SD3
    > SD2 SD1 SD0 T4 T3 T2 T1 T0
    >
    > I am reading in the two bytes using the following code
    >
    > import struct
    > f.seek(0)
    > element1_format = struct.Struct("<H")
    > (element1,) = element1_format.unpack(f.read(2))
    >
    > Now element1 has type "str" .


    That's not what I get. For me it's an int.

    > How do I apply a bitmask to this to get
    > at information in the component bits .
    > Since the entire file format has many such bitmasked fields and since
    > this is my first venture into binary formats and c-type structs , I
    > wanted to know how to read values inside a byte using python.
    > My few tries at using bitwise operators ( element1 & 0x001f) are
    > frustrated by messages that say " unsupported operand type(s) for &:
    > 'str' and 'int' " .
    > How can I keep my string objects as bits and apply bitmasks to them
    > Any help in this will be greatly appreciated.
    >

    The T bits are (element1 & 0x1F), the SD bits are ((element1 >> 5) &
    0x2FF) and the SN16 bit is (element1 >> 15).
     
    MRAB, Feb 24, 2009
    #2
    1. Advertising

  3. Re: getting at individual bits inside byte field: struct module :bitwise operator

    En Mon, 23 Feb 2009 22:18:31 -0200, harijay <> escribió:

    > mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)
    >
    > I have a question about how to bitmask a bunch of bytes read in from
    > such a binary formatted file .
    >
    > For eg the spec says the first two bytes have different parameters in
    > different bits .
    > Byte 1 Byte 0
    > SN16 SD9 SD8 SD7 SD6 SD5 SD4 SD3
    > SD2 SD1 SD0 T4 T3 T2 T1 T0
    >
    > I am reading in the two bytes using the following code
    >
    > import struct
    > f.seek(0)
    > element1_format = struct.Struct("<H")
    > (element1,) = element1_format.unpack(f.read(2))
    >
    > Now element1 has type "str" .


    No, should be type 'int', that's what the H format means.

    > How do I apply a bitmask to this to get
    > at information in the component bits .
    > Since the entire file format has many such bitmasked fields and since
    > this is my first venture into binary formats and c-type structs , I
    > wanted to know how to read values inside a byte using python.
    > My few tries at using bitwise operators ( element1 & 0x001f) are
    > frustrated by messages that say " unsupported operand type(s) for &:
    > 'str' and 'int' " .


    Try again - it should work. Perhaps you used the wrong variable name?
    instead of the resultant from struct.unpack

    py> import struct
    py> element1_format = struct.Struct("<H")
    py> (element1,) = element1_format.unpack('AB')
    py> element1
    16961
    py> element1 & 0x007F
    65
    py> chr(65)
    'A'

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 24, 2009
    #3
  4. harijay

    Mark Tolonen Guest

    Re: getting at individual bits inside byte field: struct module : bitwise operator

    "harijay" <> wrote in message
    news:...
    > In my last post I had asked about reading data from a binary file
    > using the struct module.
    > Thanks to some excellent help , I have managed to read in
    > successfully
    > most of the header of this binary format that I want to parse. These
    > are some time-voltage traces from a digital
    > to analog converter for my experiments. The output is according to a
    > format
    > mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)
    >
    > I have a question about how to bitmask a bunch of bytes read in from
    > such a binary formatted file .
    >
    > For eg the spec says the first two bytes have different parameters in
    > different bits .
    > Byte 1 Byte 0
    > SN16 SD9 SD8 SD7 SD6 SD5 SD4 SD3
    > SD2 SD1 SD0 T4 T3 T2 T1 T0
    >
    > I am reading in the two bytes using the following code
    >
    > import struct
    > f.seek(0)
    > element1_format = struct.Struct("<H")
    > (element1,) = element1_format.unpack(f.read(2))
    >
    > Now element1 has type "str" . How do I apply a bitmask to this to get
    > at information in the component bits .
    > Since the entire file format has many such bitmasked fields and since
    > this is my first venture into binary formats and c-type structs , I
    > wanted to know how to read values inside a byte using python.
    > My few tries at using bitwise operators ( element1 & 0x001f) are
    > frustrated by messages that say " unsupported operand type(s) for &:
    > 'str' and 'int' " .
    > How can I keep my string objects as bits and apply bitmasks to them
    > Any help in this will be greatly appreciated.
    > Thanks
    > hari


    Please post a small example that fails as you describe. From your example,
    element1 should be an int (unpacked from two bytes), and there is no f
    defined. Without the actual code we can't figure out what you are doing
    wrong.

    This works as expected:

    >>> import struct
    >>> format = struct.Struct("<H")
    >>> (element1,) = format.unpack('aa') # a two-byte string to unpack
    >>> print hex(element1)

    0x6161
    >>> print element1 & 0x1f

    1

    -Mark
     
    Mark Tolonen, Feb 24, 2009
    #4
  5. harijay

    harijay Guest

    Re: getting at individual bits inside byte field: struct module :bitwise operator

    Thanks Gabriel , MRAB and Mark

    Instead of using the bitwise operator on the unpacked data I was
    trying to do a bitwise operator directly after read. This was giving
    the unsupported operand error

    i.e

    # version in my code - wrong
    file.read(2) & 0x001f

    Instead of

    # version in my example and now in my working code
    (element1,) = struct.unpack("<H",file.read(2) )
    Tonethrufour = element1 & 0x1F

    I only realized it after I read your replies and went back to my
    actual code which had the mistake .
    Thanks a lot again.

    Hari



    Thanks for your help and hope I dont commit any more such stupid
    mistakes

    On Feb 23, 11:27 pm, "Mark Tolonen" <> wrote:
    > "harijay" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > In my last post I had asked about reading data from a binary file
    > > using the struct module.
    > > Thanks to some excellent help , I have managed to read in
    > > successfully
    > > most of the header of this binary format that I want to parse.  These
    > > are some time-voltage traces from a digital
    > > to analog converter for my experiments. The output is according to a
    > > format
    > > mentioned here : (http://www.dataq.com/support/techinfo/ff.htm)

    >
    > > I have a question about how to bitmask a bunch of bytes read in from
    > > such a binary formatted file .

    >
    > > For eg the spec says the first two bytes have different parameters in
    > > different bits .
    > > Byte 1                                                  Byte 0
    > > SN16 SD9        SD8     SD7     SD6     SD5     SD4     SD3
    > > SD2     SD1     SD0     T4      T3      T2      T1      T0

    >
    > > I am reading in the two bytes using the following code

    >
    > > import struct
    > > f.seek(0)
    > > element1_format = struct.Struct("<H")
    > > (element1,) = element1_format.unpack(f.read(2))

    >
    > > Now element1 has type "str" . How do I apply a bitmask to this to get
    > > at information in the component bits .
    > > Since the entire file format has many such bitmasked fields and since
    > > this is my first venture into binary formats and c-type structs , I
    > > wanted to know how to read values inside a byte using python.
    > > My few tries at using bitwise operators ( element1 & 0x001f) are
    > > frustrated by messages that say " unsupported operand type(s) for &:
    > > 'str' and 'int' " .
    > > How can I keep my string objects as bits and apply bitmasks to them
    > > Any help in this will be greatly appreciated.
    > > Thanks
    > > hari

    >
    > Please post a small example that fails as you describe.  From your example,
    > element1 should be an int (unpacked from two bytes), and there is no f
    > defined.  Without the actual code we can't figure out what you are doing
    > wrong.
    >
    > This works as expected:
    >
    > >>> import struct
    > >>> format = struct.Struct("<H")
    > >>> (element1,) = format.unpack('aa') # a two-byte string to unpack
    > >>> print hex(element1)

    > 0x6161
    > >>> print element1 & 0x1f

    >
    > 1
    >
    > -Mark
     
    harijay, Feb 24, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. benn
    Replies:
    5
    Views:
    528
    Joseph Kesselman
    Mar 20, 2008
  2. Hybr1dz
    Replies:
    6
    Views:
    2,467
    Hybr1dz
    Apr 2, 2008
  3. Daku
    Replies:
    6
    Views:
    6,639
  4. googler

    Accessing individual bits in a number

    googler, May 19, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    109
    googler
    May 19, 2008
  5. harijay
    Replies:
    2
    Views:
    141
    harijay
    Feb 24, 2009
Loading...

Share This Page