Bit shifting vs Bitfields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #16
    Re: Bit shifting vs Bitfields

    Roberto Waltman <usenet@rwaltma n.net> writes:[color=blue]
    > richard_l@latte r.demon.co.uk wrote:
    > My main query was if there were any benefits to[color=green]
    >>bit shifting rather than defining bit fields. I think my conclusion is
    >>that unless you are porting code, defining bitfields are fine. I
    >>would suggest that most ANSI compilers these days (I'm using gcc)
    >>handle bitfields in the appropriate manner.[/color]
    >
    > I agree with the "appropriat e handling", but not with "bit fields
    > unless porting".
    >
    > I would use bit fields only if all of the following three assumptions
    > are true:
    >
    > (a) It does not matter what bits are assigned to which field. [1]
    > (b) The size of the structure containing the bit fields is not
    > important. [2]
    > (c) The alignment of a bit field structure is not important. [3][/color]

    *or*

    The compiler precisely documents how it lays out bit fields, and I
    know I'll never need to worry about portability to any other compiler
    that doesn't make the same guarantees. Yes, masks and shifts will do
    the same job, but bit fields are easier to work with.

    (A good argument can be made that masks and shifts are a better idea
    even in these circumstances.)


    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Chris Torek

      #17
      Re: Bit shifting vs Bitfields

      >Roberto Waltman wrote:[color=blue][color=green]
      >> struct com_ctrl
      >> {
      >> unsigned int e:1; /* enable and rate. using one letter */
      >> unsigned int r:2; /* identifiers to fit in the */
      >> }; /* diagrams below */
      >> could be mapped into [various pictures, snipped][/color][/color]

      In article <4fbe3gF1ie9o4U 1@individual.ne t>
      Ian Collins <ian-news@hotmail.co m> wrote:[color=blue]
      >But is it?[/color]

      I have written drivers for two particular chips (one AMD Ethernet
      chip and one Zilog serial port chip) that have been used on MIPS
      ("both endian" and one of them had only 32-bit load/store), SPARC,
      Intel, and other CPUs. These drivers did not use bitfields,
      despite their apparent convenience, because those bitfields were
      in fact mapped differently on those different machines.

      In general, using bitfields buys you a little convenience, which
      you later pay for in effort porting the driver to new compilers
      and machines. Sometimes this tradeoff is worth it and sometimes
      it is not.
      --
      In-Real-Life: Chris Torek, Wind River Systems
      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
      email: forget about it http://web.torek.net/torek/index.html
      Reading email is like searching for food in the garbage, thanks to spammers.

      Comment

      • Ian Collins

        #18
        Re: Bit shifting vs Bitfields

        Keith Thompson wrote:[color=blue]
        > Roberto Waltman <usenet@rwaltma n.net> writes:
        >[color=green]
        >>richard_l@lat ter.demon.co.uk wrote:
        >>My main query was if there were any benefits to
        >>[color=darkred]
        >>>bit shifting rather than defining bit fields. I think my conclusion is
        >>>that unless you are porting code, defining bitfields are fine. I
        >>>would suggest that most ANSI compilers these days (I'm using gcc)
        >>>handle bitfields in the appropriate manner.[/color]
        >>
        >>I agree with the "appropriat e handling", but not with "bit fields
        >>unless porting".
        >>
        >>I would use bit fields only if all of the following three assumptions
        >>are true:
        >>
        >>(a) It does not matter what bits are assigned to which field. [1]
        >>(b) The size of the structure containing the bit fields is not
        >>important. [2]
        >>(c) The alignment of a bit field structure is not important. [3][/color]
        >
        >
        > *or*
        >
        > The compiler precisely documents how it lays out bit fields, and I
        > know I'll never need to worry about portability to any other compiler
        > that doesn't make the same guarantees. Yes, masks and shifts will do
        > the same job, but bit fields are easier to work with.
        >[/color]
        That's been my situation, embedded targets where the toolchain is very
        unlikely to change.

        --
        Ian Collins.

        Comment

        • Roberto Waltman

          #19
          Re: Bit shifting vs Bitfields

          Ian Collins <ian-news@hotmail.co m> wrote:[color=blue]
          >Keith Thompson wrote:[color=green]
          >>...
          >> The compiler precisely documents how it lays out bit fields, and I
          >> know I'll never need to worry about portability to any other compiler
          >> that doesn't make the same guarantees. Yes, masks and shifts will do
          >> the same job, but bit fields are easier to work with.
          >>[/color]
          >That's been my situation, embedded targets where the toolchain is very
          >unlikely to change.[/color]

          OK, I get it now. I agree, the toolchain used for an embedded project
          is not likely to change. My comments were in the context of code that
          may be ported to different platforms. (Or, for communication
          protocols, shared among different platforms.)

          Comment

          Working...