Writing C readable bitfield structs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phark52@yahoo.com

    #1

    Writing C readable bitfield structs?

    How would I go about writing a bitfield that can be read by my C app? I
    want to pack a few bools into one int.

    I know an extended module exists (npstruct) which helps you do this but
    I want to do it manually or using one of the standard modules.

  • Larry Bates

    #2
    Re: Writing C readable bitfield structs?

    phark52@yahoo.c om wrote:[color=blue]
    > How would I go about writing a bitfield that can be read by my C app? I
    > want to pack a few bools into one int.
    >
    > I know an extended module exists (npstruct) which helps you do this but
    > I want to do it manually or using one of the standard modules.
    >[/color]
    struct.pack is in the Standard Library and allows you to do what
    you want. You may find that you must also do some "bit twiddling"
    using << shift functions and | bit or function if you want to pack
    tighter than on 4 bit boundaries.

    Larry Bates

    Comment

    • Cappy2112

      #3
      Re: Writing C readable bitfield structs?

      there is a bitfiled mainpulator class inthe Cookbook, but I don't
      understand his explanation, and the example given doesn't really show
      off the features of the class.

      I too need bit-level manipulation, and will probably have to write my
      own class to do it.

      Comment

      • Roy Smith

        #4
        Re: Writing C readable bitfield structs?

        In article <1111002678.866 378.102970@l41g 2000cwc.googleg roups.com>,
        Cappy2112 <cappy2112@gmai l.com> wrote:[color=blue]
        >there is a bitfiled mainpulator class inthe Cookbook, but I don't
        >understand his explanation, and the example given doesn't really show
        >off the features of the class.[/color]

        I assume you're talking about the struct module? If you give an
        example of the C struct you're trying to read/write, I could come up
        with some sample code to do it.

        Comment

        • phark52@yahoo.com

          #5
          Re: Writing C readable bitfield structs?


          Roy Smith wrote:[color=blue]
          > In article <1111002678.866 378.102970@l41g 2000cwc.googleg roups.com>,
          > Cappy2112 <cappy2112@gmai l.com> wrote:[color=green]
          > >there is a bitfiled mainpulator class inthe Cookbook, but I don't
          > >understand his explanation, and the example given doesn't really[/color][/color]
          show[color=blue][color=green]
          > >off the features of the class.[/color]
          >
          > I assume you're talking about the struct module? If you give an
          > example of the C struct you're trying to read/write, I could come up
          > with some sample code to do it.[/color]

          struct S {
          unsigned int a : 1;
          unsigned int b : 1;
          unsigned int c : 1;
          unsigned int d : 1;
          };

          fread(from file (file written by Python app) into an instance of struct
          S)
          then I want it to be used as follows:
          if (instance.a) f();
          if (instance.b) g();
          struct S comes out to 4 on my arch. I do not want to use a new int for
          every member of struct S.

          Comment

          • phark52@yahoo.com

            #6
            Re: Writing C readable bitfield structs?

            Anyone have any idea?

            phark52@yahoo.c om wrote:[color=blue]
            > Roy Smith wrote:[color=green]
            > > In article <1111002678.866 378.102970@l41g 2000cwc.googleg roups.com>,
            > > Cappy2112 <cappy2112@gmai l.com> wrote:[color=darkred]
            > > >there is a bitfiled mainpulator class inthe Cookbook, but I don't
            > > >understand his explanation, and the example given doesn't really[/color][/color]
            > show[color=green][color=darkred]
            > > >off the features of the class.[/color]
            > >
            > > I assume you're talking about the struct module? If you give an
            > > example of the C struct you're trying to read/write, I could come[/color][/color]
            up[color=blue][color=green]
            > > with some sample code to do it.[/color]
            >
            > struct S {
            > unsigned int a : 1;
            > unsigned int b : 1;
            > unsigned int c : 1;
            > unsigned int d : 1;
            > };
            >
            > fread(from file (file written by Python app) into an instance of[/color]
            struct[color=blue]
            > S)
            > then I want it to be used as follows:
            > if (instance.a) f();
            > if (instance.b) g();
            > struct S comes out to 4 on my arch. I do not want to use a new int[/color]
            for[color=blue]
            > every member of struct S.[/color]

            Comment

            • John Machin

              #7
              Re: Writing C readable bitfield structs?


              phark52@yahoo.c om TOP-POSTED:[color=blue]
              > Anyone have any idea?[/color]

              1. Larry Bates has already told you.

              2. I note that you say "I do not want to use a new int for every member
              of struct S.", *not* "I am forced to pack bools into an int, 1 bit per
              bool, because I have no control over the file format". Quite a
              difference.

              3. One could ask: How you would do it in your C app, if C didn't have
              bitfields in structs?

              Never mind, I'll give a bit more detail:

              In your Python script:

              !APOS = 0; BPOS = 1; CPOS = 2
              !for each record: # pseudocode
              ! outint = 0
              ! if is_a: outint |= (1 << APOS)
              ! if is_b: outint |= (1 << BPOS)
              ! if is_c: outint |= (1 << CPOS)
              ! etc

              Note that AFAIK, C makes no guarantee about the order of bitfields in
              structs, nor whether they start at the big end or the little end of the
              int that contains them; so you may need to change the APOS etc numbers.

              If you do have control over your C app, you could use the struct module
              to pack the bools one per byte, and remove any concerns about the
              idiosyncracies of C compilers and the endianness of the source and
              target architectures. You could even make the file not only eyeballable
              but robust by representing the values as "T" and "F" instead of "\1"
              and "\0" (recalling that by ancient tradition C programs are likely to
              stuff up mightily when presented with "\0" in data).

              Comment

              Working...