<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Effective C Tip #6 &#8211; Creating a flags variable</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Thu, 09 Feb 2012 07:32:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2794</link>
		<dc:creator>Ashleigh</dc:creator>
		<pubDate>Sun, 28 Nov 2010 08:43:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2794</guid>
		<description>I&#039;m afraid I have to agree with both Nigel and Lundin on this one.

I used bit fielded named flags for the same reason as Nigel - when it makes sense to do so and it saves me space. If I want a list of bits for checking, I use an INT16 size value (with comments that its bit fielded) and use masks and loops.

Its horses for courses.

If you have ever done any ADA you will find bit fields dont exist for all the reasons of evilness cited, and this turns out to be a major pain in the neck. I spent a few happy hours writing my own Ada83 bit manipulation package because using things like arrays of booleans turns out to be hideous, as well as slow.

Moral of the story: There is no one right solution which will solve every problem known to man.</description>
		<content:encoded><![CDATA[<p>I&#8217;m afraid I have to agree with both Nigel and Lundin on this one.</p>
<p>I used bit fielded named flags for the same reason as Nigel &#8211; when it makes sense to do so and it saves me space. If I want a list of bits for checking, I use an INT16 size value (with comments that its bit fielded) and use masks and loops.</p>
<p>Its horses for courses.</p>
<p>If you have ever done any ADA you will find bit fields dont exist for all the reasons of evilness cited, and this turns out to be a major pain in the neck. I spent a few happy hours writing my own Ada83 bit manipulation package because using things like arrays of booleans turns out to be hideous, as well as slow.</p>
<p>Moral of the story: There is no one right solution which will solve every problem known to man.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2793</link>
		<dc:creator>Ashleigh</dc:creator>
		<pubDate>Sun, 28 Nov 2010 08:37:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2793</guid>
		<description>Always check the generated code.

Usualyl when using fields like this for flags the code will be larger than just accessing a plain boolean in a byte. However, if you have the common problem on a micro of plenty of flash and not enough RAM then its a case of beggars not being choosers. A little extra flash to fiddle the bits is nothing if if buys you back the RAM you need.</description>
		<content:encoded><![CDATA[<p>Always check the generated code.</p>
<p>Usualyl when using fields like this for flags the code will be larger than just accessing a plain boolean in a byte. However, if you have the common problem on a micro of plenty of flash and not enough RAM then its a case of beggars not being choosers. A little extra flash to fiddle the bits is nothing if if buys you back the RAM you need.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2792</link>
		<dc:creator>Ashleigh</dc:creator>
		<pubDate>Sun, 28 Nov 2010 08:34:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2792</guid>
		<description>I used bitfields for mapping to fields in communication protocols. Naughty, I hear you say. Only if you don&#039;t know what your compiler will do. Check the generated code, very carefully.

Using things like a 3-bit named quantity TENDS in the long run to make your code far more readable and less error prone than explicitly writing something like (buffer[x] &amp; 0xC8)&gt;&gt;3.

When you find yourself doing the above more than about  twice, there needs to be a better way. Sometimes an objecty-sort of approach (getter / setter) is the way to go but this can be horribly inefficient for run time speed if there is a function call overhead associated. A bit-fielded structure definition solve the problem. Just so long as you know what the compiler will do, document that in comments, and use the option in your makefile to force the compiler to always allocated fielded structures the way you wanted.</description>
		<content:encoded><![CDATA[<p>I used bitfields for mapping to fields in communication protocols. Naughty, I hear you say. Only if you don&#8217;t know what your compiler will do. Check the generated code, very carefully.</p>
<p>Using things like a 3-bit named quantity TENDS in the long run to make your code far more readable and less error prone than explicitly writing something like (buffer[x] &amp; 0xC8)&gt;&gt;3.</p>
<p>When you find yourself doing the above more than about  twice, there needs to be a better way. Sometimes an objecty-sort of approach (getter / setter) is the way to go but this can be horribly inefficient for run time speed if there is a function call overhead associated. A bit-fielded structure definition solve the problem. Just so long as you know what the compiler will do, document that in comments, and use the option in your makefile to force the compiler to always allocated fielded structures the way you wanted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2768</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Thu, 25 Nov 2010 10:45:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2768</guid>
		<description>To back up the arguments provided, the C safety pioneer/guru Prof. Les Hatton recommends that bit fields should be banned entirely upon implementing a safer subset of the C language. For those who don&#039;t know who he is, his work in the 90s had heavy influence on most &quot;de facto&quot; safety standards such as MISRA-C. All his work is based on actual scientific research.

Hatton, Les, Safer C: developing software for high-integrity and safety-critical systems. ISBN 0-07-707640-0.</description>
		<content:encoded><![CDATA[<p>To back up the arguments provided, the C safety pioneer/guru Prof. Les Hatton recommends that bit fields should be banned entirely upon implementing a safer subset of the C language. For those who don&#8217;t know who he is, his work in the 90s had heavy influence on most &#8220;de facto&#8221; safety standards such as MISRA-C. All his work is based on actual scientific research.</p>
<p>Hatton, Les, Safer C: developing software for high-integrity and safety-critical systems. ISBN 0-07-707640-0.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2431</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Tue, 19 Oct 2010 09:22:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2431</guid>
		<description>Ok readability. First of all, I don&#039;t buy the argument that &quot;the C language&#039;s syntax is weird, so therefore we must make a work-around&quot;. One must assume that the code is read by a competent C programmer, one who doesn&#039;t raise a brow when they see flags &amp;= ~BAR. If someone reads my code and think it is confusing because is is using the extremely common bitwise operators, they shouldn&#039;t be programming embedded systems.  

---

As for readability of bit fields: 
The whole point of using flags on bit level, rather than &quot;byte flags&quot; (typedef BOOL uint8_t), is that you want to save memory space. Since we have this requirement to save memory space for the application, we can safely assume that there is a lot of flags present (8 or more), or we wouldn&#039;t use bit fields in the first place.

Then how do you check all flags  in a loop? A very common task in my experience. With bitwise operators this is very straight-forward:

/* &quot;flags&quot; is a pure integer */
for(mask=0x01; mask!=0x00; mask&lt; 0)
  {
    do_something();
  }
}

This loop could easily be modified by some memory guru to optimize for cache memory access etc. It has the potential to become extremely efficient without sacrificing readability.

With C bit fields, you -cannot- use a loop like that, since you don&#039;t know where the bits are allocated. You -must- do like this:

/* &quot;flags&quot; is a C bit field */
BOOL do_it = FALSE;

switch(i)
{
  case SOME_CONSTANT:
    if(flags.foo &gt; 0)
    {
      do_it = TRUE;
    }
    break;
      
  case SOME_OTHER_CONSTANT:
    if(flags.bar &gt; 0)
    {
      do_it = TRUE;
    }
    break;

...
/* 30 more case statements here to check a uint32 */

    default:
}
  
if(do_it)
{
  do_something();
}

And now I&#039;d like to hear exactly how a vast amount of case statements is more readable than a 3 line loop! :)  Not to mention that code size and execution speed is far worse with the switch statement. 

---

Still, the real argument here is that bit fields come with countless amounts of pit falls. Introducing them to your program is to introduce a great bug potential, while at the same time throwing all portability away.</description>
		<content:encoded><![CDATA[<p>Ok readability. First of all, I don&#8217;t buy the argument that &#8220;the C language&#8217;s syntax is weird, so therefore we must make a work-around&#8221;. One must assume that the code is read by a competent C programmer, one who doesn&#8217;t raise a brow when they see flags &amp;= ~BAR. If someone reads my code and think it is confusing because is is using the extremely common bitwise operators, they shouldn&#8217;t be programming embedded systems.  </p>
<p>&#8212;</p>
<p>As for readability of bit fields:<br />
The whole point of using flags on bit level, rather than &#8220;byte flags&#8221; (typedef BOOL uint8_t), is that you want to save memory space. Since we have this requirement to save memory space for the application, we can safely assume that there is a lot of flags present (8 or more), or we wouldn&#8217;t use bit fields in the first place.</p>
<p>Then how do you check all flags  in a loop? A very common task in my experience. With bitwise operators this is very straight-forward:</p>
<p>/* &#8220;flags&#8221; is a pure integer */<br />
for(mask=0&#215;01; mask!=0&#215;00; mask&lt; 0)<br />
  {<br />
    do_something();<br />
  }<br />
}</p>
<p>This loop could easily be modified by some memory guru to optimize for cache memory access etc. It has the potential to become extremely efficient without sacrificing readability.</p>
<p>With C bit fields, you -cannot- use a loop like that, since you don&#8217;t know where the bits are allocated. You -must- do like this:</p>
<p>/* &#8220;flags&#8221; is a C bit field */<br />
BOOL do_it = FALSE;</p>
<p>switch(i)<br />
{<br />
  case SOME_CONSTANT:<br />
    if(flags.foo &gt; 0)<br />
    {<br />
      do_it = TRUE;<br />
    }<br />
    break;</p>
<p>  case SOME_OTHER_CONSTANT:<br />
    if(flags.bar &gt; 0)<br />
    {<br />
      do_it = TRUE;<br />
    }<br />
    break;</p>
<p>&#8230;<br />
/* 30 more case statements here to check a uint32 */</p>
<p>    default:<br />
}</p>
<p>if(do_it)<br />
{<br />
  do_something();<br />
}</p>
<p>And now I&#8217;d like to hear exactly how a vast amount of case statements is more readable than a 3 line loop! <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Not to mention that code size and execution speed is far worse with the switch statement. </p>
<p>&#8212;</p>
<p>Still, the real argument here is that bit fields come with countless amounts of pit falls. Introducing them to your program is to introduce a great bug potential, while at the same time throwing all portability away.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2426</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Mon, 18 Oct 2010 12:00:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2426</guid>
		<description>I think where we differ is on the use of flags. When I use a flags variable as described here, all I&#039;m interested in doing is grouping a number of related boolean values. All I ever do is set / clear a flag and test it for true / false. I suppose there has been the odd time in which I have set one flag equal to another. For example:
flags.foo = flags.bar;
However, in this case I don&#039;t think there is much ambiguity that flags.foo will end up with the value of flags.bar.

Anyway, if this is how one uses flags variables, then it doesn&#039;t matter where the compiler places them in memory. What matters to me is that I can now write very clear code. For example:
if (flags.foo)
{
  flags.bar = 0;
}

Doing the same with bit masks and macros, the above code tends to look something like this:
if (flags &amp; BIT(3))
{
 flags &amp;= ~BIT(6);
}
Clearly one can define some intermediate macros. For example:
#define FOO BIT(3)
#define BAR BIT(6)
if (flags &amp; FOO)
{
 flags &amp;= ~BAR;
}
However IMHO it still isn&#039;t  as clear as the bit field representation. I&#039;d also note that for code like this to be reliable, one has to be very careful about the definition of the BIT() macro. There&#039;s also the issue of the underlying type of the flags variable. For example:
uint8_t flags;
...
flags &#124;= BIT(9);
Good compilers will warn you that this operation is useless. Not so good compilers will happily let you do this. Of course Lint will catch it :-)</description>
		<content:encoded><![CDATA[<p>I think where we differ is on the use of flags. When I use a flags variable as described here, all I&#8217;m interested in doing is grouping a number of related boolean values. All I ever do is set / clear a flag and test it for true / false. I suppose there has been the odd time in which I have set one flag equal to another. For example:<br />
flags.foo = flags.bar;<br />
However, in this case I don&#8217;t think there is much ambiguity that flags.foo will end up with the value of flags.bar.</p>
<p>Anyway, if this is how one uses flags variables, then it doesn&#8217;t matter where the compiler places them in memory. What matters to me is that I can now write very clear code. For example:<br />
if (flags.foo)<br />
{<br />
  flags.bar = 0;<br />
}</p>
<p>Doing the same with bit masks and macros, the above code tends to look something like this:<br />
if (flags &amp; BIT(3))<br />
{<br />
 flags &amp;= ~BIT(6);<br />
}<br />
Clearly one can define some intermediate macros. For example:<br />
#define FOO BIT(3)<br />
#define BAR BIT(6)<br />
if (flags &amp; FOO)<br />
{<br />
 flags &amp;= ~BAR;<br />
}<br />
However IMHO it still isn&#8217;t  as clear as the bit field representation. I&#8217;d also note that for code like this to be reliable, one has to be very careful about the definition of the BIT() macro. There&#8217;s also the issue of the underlying type of the flags variable. For example:<br />
uint8_t flags;<br />
&#8230;<br />
flags |= BIT(9);<br />
Good compilers will warn you that this operation is useless. Not so good compilers will happily let you do this. Of course Lint will catch it <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2424</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Mon, 18 Oct 2010 09:35:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2424</guid>
		<description>Perhaps needless to say, this is one of my pet peeves :)

How can something be readable when you have no idea where the bits end up in the memory cells? Assuming that the reader doesn&#039;t have in-depth knowledge of the specific compiler&#039;s bit field handling.

Flags.bar = 1U; 

From this I can tell that &quot;bar&quot; is given the value 1. I have no idea whatsoever where &quot;bar&quot; is allocated, or how much space that is allocated by the structure it resides in. That might be sufficient for the average PC programmer, but not for them picky embedded folks who want to know exactly where all ones and zeroes end up.

But the real problems begin when I do like this:

uint8_t x = Flags.bar;

There is just no way I can know what value the uint8 has after that line, unless I read the compiler front-end documents. And what about this:

uint8_t value;
uint8_t mask;
...
if((value &amp; Flags.bar) == mask)

Even if &quot;Flags&quot; was allocated as an ISO C bit field, I still doubt very few C programmers can tell you straight away what this code actually results in, because of the implicit integer promotion madness that the compiler will conjure. I cannot, not without scratching my head for at least half an hour and reading up on both the C standard and the compiler docs.

If I were using the bitwise operators instead, nothing would be ambiguous, and I wouldn&#039;t need to use the struct notation either. Then I could read/write the same code in one minute and the code would be completely deterministic under any C compiler for any system.</description>
		<content:encoded><![CDATA[<p>Perhaps needless to say, this is one of my pet peeves <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>How can something be readable when you have no idea where the bits end up in the memory cells? Assuming that the reader doesn&#8217;t have in-depth knowledge of the specific compiler&#8217;s bit field handling.</p>
<p>Flags.bar = 1U; </p>
<p>From this I can tell that &#8220;bar&#8221; is given the value 1. I have no idea whatsoever where &#8220;bar&#8221; is allocated, or how much space that is allocated by the structure it resides in. That might be sufficient for the average PC programmer, but not for them picky embedded folks who want to know exactly where all ones and zeroes end up.</p>
<p>But the real problems begin when I do like this:</p>
<p>uint8_t x = Flags.bar;</p>
<p>There is just no way I can know what value the uint8 has after that line, unless I read the compiler front-end documents. And what about this:</p>
<p>uint8_t value;<br />
uint8_t mask;<br />
&#8230;<br />
if((value &amp; Flags.bar) == mask)</p>
<p>Even if &#8220;Flags&#8221; was allocated as an ISO C bit field, I still doubt very few C programmers can tell you straight away what this code actually results in, because of the implicit integer promotion madness that the compiler will conjure. I cannot, not without scratching my head for at least half an hour and reading up on both the C standard and the compiler docs.</p>
<p>If I were using the bitwise operators instead, nothing would be ambiguous, and I wouldn&#8217;t need to use the struct notation either. Then I could read/write the same code in one minute and the code would be completely deterministic under any C compiler for any system.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2395</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Wed, 13 Oct 2010 22:49:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2395</guid>
		<description>Thanks for the input Daniel. I think the issue you raised about portability is an interesting one. I haven&#039;t opined on portability much in this blog, so perhaps it&#039;s time I did. If portability is not an issue, how do you compare the readability of bit fields with bit masking in this application?</description>
		<content:encoded><![CDATA[<p>Thanks for the input Daniel. I think the issue you raised about portability is an interesting one. I haven&#8217;t opined on portability much in this blog, so perhaps it&#8217;s time I did. If portability is not an issue, how do you compare the readability of bit fields with bit masking in this application?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2390</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Wed, 13 Oct 2010 15:12:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2390</guid>
		<description>Nigel&#039;s advises are almost always wise and sound, but this one is not. There are so many problems in that code, I don&#039;t know where to start...

The most blatant one is indeed that the code will not compile under a standard C compiler. As Nigel points out, bit fields can only be of type int, signed int or unsigned int. Sure, there are many many non-standard compilers out there. Just because they allow some inane non-standard syntax doesn&#039;t mean it should be used. Good code == portable code, especially in embedded systems.

The next mistake is to use structs/unions for bit mapping. According to the standard, the compiler may add any number of padding bytes into a struct/union, and bit fields are no exception to this rule. This is the main reason MISRA bans unions. So all we know about that union is that its size &gt;= 1 byte. If you use an array of such struct/unions, you are asking for trouble. 

The royal mistake is to use bit fields in the first place. The following is not specified by the standard: 

- whether bit field int is treated as signed or unsigned int
- the bit order (lsb is where?)
- whether the bit field can reach past the memory alignment of the CPU or not
- the alignment of non-bit field members of the struct
- the memory alignment of bit fields (to the left or to the right?)
- the endianess of bit fields larger than one byte
- whether plain int values assigned to them are interpreted as signed or unsigned
- how bit fields are promoted implicitly by the integer promotions
- whether signed bit fields are one&#039;s compliment or two&#039;s compliment
- padding bytes
- padding bits
- values of padding bits. 
- and so on.

The only thing you know when declaring a bit field is that you have an unportable chunk of random data of a random size, that the program may use in random ways.

Flags.bar = 1U; /* Set the bar flag */

If we ignore the fact that 8-bit bit fields aren&#039;t even valid C, here is just a few examples of what result this line of code will yield:

0x02
0x40
0x0002
0x4000
0x0200
0x0040
0x00000002
0x40000000
0x02000000
0x40000000
0x02ABCDEF
0x40ABCDEF

Most important of all, bit fields will never be more efficient than plain bit-wise operators, but can be less efficient in many ways. Though of course, you have to write a PhD thesis about bit fields before you can actually tell what code they will yield, since they are so incredibly poorly defined.

Don&#039;t use bit fields ever.</description>
		<content:encoded><![CDATA[<p>Nigel&#8217;s advises are almost always wise and sound, but this one is not. There are so many problems in that code, I don&#8217;t know where to start&#8230;</p>
<p>The most blatant one is indeed that the code will not compile under a standard C compiler. As Nigel points out, bit fields can only be of type int, signed int or unsigned int. Sure, there are many many non-standard compilers out there. Just because they allow some inane non-standard syntax doesn&#8217;t mean it should be used. Good code == portable code, especially in embedded systems.</p>
<p>The next mistake is to use structs/unions for bit mapping. According to the standard, the compiler may add any number of padding bytes into a struct/union, and bit fields are no exception to this rule. This is the main reason MISRA bans unions. So all we know about that union is that its size &gt;= 1 byte. If you use an array of such struct/unions, you are asking for trouble. </p>
<p>The royal mistake is to use bit fields in the first place. The following is not specified by the standard: </p>
<p>- whether bit field int is treated as signed or unsigned int<br />
- the bit order (lsb is where?)<br />
- whether the bit field can reach past the memory alignment of the CPU or not<br />
- the alignment of non-bit field members of the struct<br />
- the memory alignment of bit fields (to the left or to the right?)<br />
- the endianess of bit fields larger than one byte<br />
- whether plain int values assigned to them are interpreted as signed or unsigned<br />
- how bit fields are promoted implicitly by the integer promotions<br />
- whether signed bit fields are one&#8217;s compliment or two&#8217;s compliment<br />
- padding bytes<br />
- padding bits<br />
- values of padding bits.<br />
- and so on.</p>
<p>The only thing you know when declaring a bit field is that you have an unportable chunk of random data of a random size, that the program may use in random ways.</p>
<p>Flags.bar = 1U; /* Set the bar flag */</p>
<p>If we ignore the fact that 8-bit bit fields aren&#8217;t even valid C, here is just a few examples of what result this line of code will yield:</p>
<p>0&#215;02<br />
0&#215;40<br />
0&#215;0002<br />
0&#215;4000<br />
0&#215;0200<br />
0&#215;0040<br />
0&#215;00000002<br />
0&#215;40000000<br />
0&#215;02000000<br />
0&#215;40000000<br />
0x02ABCDEF<br />
0x40ABCDEF</p>
<p>Most important of all, bit fields will never be more efficient than plain bit-wise operators, but can be less efficient in many ways. Though of course, you have to write a PhD thesis about bit fields before you can actually tell what code they will yield, since they are so incredibly poorly defined.</p>
<p>Don&#8217;t use bit fields ever.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Brown</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-6-creating-a-flags-variable/comment-page-1/#comment-2165</link>
		<dc:creator>David Brown</dc:creator>
		<pubDate>Mon, 27 Sep 2010 15:13:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/01/effective-c-tip-6-creating-a-flags-variable/#comment-2165</guid>
		<description>gcc comes in many versions, for many targets.  For some targets, especially using older versions of gcc, the generated code is poor.  In most cases with modern versions, the generated code is good or optimal.  The same applies to many commercial compilers.

Certainly if you are using flag registers like this to get smaller or faster code (or use less RAM space), then it&#039;s important to test it out and check the generated assembly from your compiler.  If you are unsure about the bit ordering, then again this is something to check.

In the end, it&#039;s a style choice.  The most important factor is that it produces correct code, and that it is easy to read and understand.  If it produces good code from your compiler, then that&#039;s useful too!</description>
		<content:encoded><![CDATA[<p>gcc comes in many versions, for many targets.  For some targets, especially using older versions of gcc, the generated code is poor.  In most cases with modern versions, the generated code is good or optimal.  The same applies to many commercial compilers.</p>
<p>Certainly if you are using flag registers like this to get smaller or faster code (or use less RAM space), then it&#8217;s important to test it out and check the generated assembly from your compiler.  If you are unsure about the bit ordering, then again this is something to check.</p>
<p>In the end, it&#8217;s a style choice.  The most important factor is that it produces correct code, and that it is easy to read and understand.  If it produces good code from your compiler, then that&#8217;s useful too!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

