<?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: Binary Literals in C</title>
	<atom:link href="http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/</link>
	<description>A Blog by Michael Barr</description>
	<lastBuildDate>Tue, 24 Jan 2012 21:58:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Consulting as a leading economic indicator &#8211; update #1 &#171; Stack Overflow</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-331</link>
		<dc:creator>Consulting as a leading economic indicator &#8211; update #1 &#171; Stack Overflow</dc:creator>
		<pubDate>Tue, 30 Mar 2010 11:13:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-331</guid>
		<description>[...] topics, if you have not read Mike Barr&#8217;s recent posting on binary literals, then I strongly recommend that you do so. It would have fitted very nicely into [...]</description>
		<content:encoded><![CDATA[<p>[...] topics, if you have not read Mike Barr&#8217;s recent posting on binary literals, then I strongly recommend that you do so. It would have fitted very nicely into [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miro Samek</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-77</link>
		<dc:creator>Miro Samek</dc:creator>
		<pubDate>Tue, 13 Oct 2009 14:40:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-77</guid>
		<description>Indeed, MISRA checker does not particularly like the macros, because they violate the following MISRA rules:macro HEX__(n) Violates MISRA Required Rule 98, Multiple use of &#039;#/##&#039;    operators in macro definitionmacro B8__ Violates MISRA Rule 7, Trigraphs shall not be usedmacro B8__ Violates MISRA Rule 96, Expression-like macro &#039;&#039; not parenthesizedBut the macros can be restructured as follows to comply with MISRA:#define HEX__(n_) ((uint32_t)0x##n_)#define B8__(x_) \    (  ((x_) &amp; 0x1LU) \     &#124; (((x_) &amp; 0x10LU) &gt;&gt; 3) \     &#124; (((x_) &amp; 0x100LU) &gt;&gt; 6) \     &#124; (((x_) &amp; 0x1000LU) &gt;&gt; 9) \     &#124; (((x_) &amp; 0x10000LU) &gt;&gt; 12) \     &#124; (((x_) &amp; 0x100000LU) &gt;&gt; 15) \     &#124; (((x_) &amp; 0x1000000LU) &gt;&gt; 18) \     &#124; (((x_) &amp; 0x10000000LU) &gt;&gt; 21))#define B8(b0_) ((uint8_t)B8__(HEX__(b0_)))#define B16(b1_,b0_) \    ((uint16_t)(B8__(HEX__(b0_)) &#124; (B8__(HEX__(b1_)) &lt;&lt; 8)))#define B32(b3_,b2_,b1_,b0_) \    ((uint32_t)(B8__(HEX__(b0_)) \             &#124; (B8__(HEX__(b1_)) &lt;&lt; 8) \             &#124; (B8__(HEX__(b2_)) &lt;&lt; 16) \             &#124; (B8__(HEX__(b3_)) &lt;&lt; 24)))While the macros are MISRA compliant, the actual use can sometimes lead to violating MISRA rule 19 (octal constant used), if you start your binary literal with a zero. Miro Samek</description>
		<content:encoded><![CDATA[<p>Indeed, MISRA checker does not particularly like the macros, because they violate the following MISRA rules:macro HEX__(n) Violates MISRA Required Rule 98, Multiple use of &#39;#/##&#39;    operators in macro definitionmacro B8__ Violates MISRA Rule 7, Trigraphs shall not be usedmacro B8__ Violates MISRA Rule 96, Expression-like macro &#39;&#39; not parenthesizedBut the macros can be restructured as follows to comply with MISRA:#define HEX__(n_) ((uint32_t)0x##n_)#define B8__(x_) \    (  ((x_) &amp; 0x1LU) \     | (((x_) &amp; 0x10LU) &gt;&gt; 3) \     | (((x_) &amp; 0x100LU) &gt;&gt; 6) \     | (((x_) &amp; 0x1000LU) &gt;&gt; 9) \     | (((x_) &amp; 0x10000LU) &gt;&gt; 12) \     | (((x_) &amp; 0x100000LU) &gt;&gt; 15) \     | (((x_) &amp; 0x1000000LU) &gt;&gt; 18) \     | (((x_) &amp; 0x10000000LU) &gt;&gt; 21))#define B8(b0_) ((uint8_t)B8__(HEX__(b0_)))#define B16(b1_,b0_) \    ((uint16_t)(B8__(HEX__(b0_)) | (B8__(HEX__(b1_)) &lt;&lt; 8)))#define B32(b3_,b2_,b1_,b0_) \    ((uint32_t)(B8__(HEX__(b0_)) \             | (B8__(HEX__(b1_)) &lt;&lt; <img src='http://embeddedgurus.com/barr-code/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> \             | (B8__(HEX__(b2_)) &lt;&lt; 16) \             | (B8__(HEX__(b3_)) &lt;&lt; 24)))While the macros are MISRA compliant, the actual use can sometimes lead to violating MISRA rule 19 (octal constant used), if you start your binary literal with a zero. Miro Samek</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-76</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Mon, 12 Oct 2009 13:44:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-76</guid>
		<description>Sure can. By way of example, lets look at B8(1011).First off the HEX__ macro converts the argument 1011 into 0x1011UL. It does this via the stringize operator ##. (This is a pretty obscure part of the preprocessor - you&#039;ll find more information online).Now let&#039;s see what happens with the B8__(x) macro. It consists of 8 lines, each of which effectively tests one bit position. The first line is (x &amp; 0x0000000Flu) ? 1 : 0. (Note that I&#039;ve added spaces and made the LU lower case, thus making things a little clearer IMHO).Anyway, this becomes: 0x1011ul &amp; 0x0000000Ful = 0x1. This evidently evaluates to true and so the ternary operator returns 1.The second line of the B8__() macro is (x &amp; 0x000000F0lu) ? 2 : 0). Thus this expands to 0x1011 &amp; 0x000000F0 = 0x10. This evidently evaluates to true and so the ternary operator returns 2, which is added to the previous result, giving a total of decimal 3.The third line of the B8__() macro is (x &amp; 0x00000F00lu) ? 4 : 0). Thus this expands to 0x1011 &amp; 0x00000F0 = 0x0. This evidently evaluates to false and so the ternary operator returns 0, which is added to the previous result, keeping our total at decimal 3.The fourth line of the B8__() macro is (x &amp; 0x0000F000lu) ? 8 : 0). Thus this expands to 0x1011 &amp; 0x0000F000 = 0x1000. This evidently evaluates to true and so the ternary operator returns 8, which is added to the previous result, giving a total of decimal 11 - and our final result.The B16 and B32 macros simply build upon this technique by using appropriate casts and shifts.Hope this helps. If not post again and I&#039;ll try and expand upon the explanation.</description>
		<content:encoded><![CDATA[<p>Sure can. By way of example, lets look at B8(1011).First off the HEX__ macro converts the argument 1011 into 0x1011UL. It does this via the stringize operator ##. (This is a pretty obscure part of the preprocessor &#8211; you&#39;ll find more information online).Now let&#39;s see what happens with the B8__(x) macro. It consists of 8 lines, each of which effectively tests one bit position. The first line is (x &amp; 0x0000000Flu) ? 1 : 0. (Note that I&#39;ve added spaces and made the LU lower case, thus making things a little clearer IMHO).Anyway, this becomes: 0x1011ul &amp; 0x0000000Ful = 0&#215;1. This evidently evaluates to true and so the ternary operator returns 1.The second line of the B8__() macro is (x &amp; 0x000000F0lu) ? 2 : 0). Thus this expands to 0&#215;1011 &amp; 0x000000F0 = 0&#215;10. This evidently evaluates to true and so the ternary operator returns 2, which is added to the previous result, giving a total of decimal 3.The third line of the B8__() macro is (x &amp; 0x00000F00lu) ? 4 : 0). Thus this expands to 0&#215;1011 &amp; 0x00000F0 = 0&#215;0. This evidently evaluates to false and so the ternary operator returns 0, which is added to the previous result, keeping our total at decimal 3.The fourth line of the B8__() macro is (x &amp; 0x0000F000lu) ? 8 : 0). Thus this expands to 0&#215;1011 &amp; 0x0000F000 = 0&#215;1000. This evidently evaluates to true and so the ternary operator returns 8, which is added to the previous result, giving a total of decimal 11 &#8211; and our final result.The B16 and B32 macros simply build upon this technique by using appropriate casts and shifts.Hope this helps. If not post again and I&#39;ll try and expand upon the explanation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kenneth</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-75</link>
		<dc:creator>Kenneth</dc:creator>
		<pubDate>Mon, 12 Oct 2009 13:13:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-75</guid>
		<description>I&#039;m trying to learn the finer points of C and how to be creative with it.  :)I don&#039;t understand something the binary macros. ----------------------------/* 8-bit conversion function */#define B8__(x) ((x&amp;0x0000000FLU)?1:0) \+((x&amp;0x000000F0LU)?2:0) \+((x&amp;0x00000F00LU)?4:0) \+((x&amp;0x0000F000LU)?8:0) \+((x&amp;0x000F0000LU)?16:0) \+((x&amp;0x00F00000LU)?32:0) \+((x&amp;0x0F000000LU)?64:0) \+((x&amp;0xF0000000LU)?128:0)-------------------------------If we take part of the first line:(x&amp;0x0000000FLU)It would appear to me that x is being anded with the constant 0x0000000FLU.Its with this constant that I get lost.  To me 0x0000000F = 15 (in decimal).  The &quot;LU&quot; as I underatand it, and I could be wrong, signifies the constant is an Unsigned Long.I understand how the macro is supposed to work.  My problem, I guess is with the syntax.Can someone shed some light?Thanks,Kennykenl@anspach.com</description>
		<content:encoded><![CDATA[<p>I&#39;m trying to learn the finer points of C and how to be creative with it.  <img src='http://embeddedgurus.com/barr-code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I don&#39;t understand something the binary macros. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-/* 8-bit conversion function */#define B8__(x) ((x&amp;0x0000000FLU)?1:0) \+((x&amp;0x000000F0LU)?2:0) \+((x&amp;0x00000F00LU)?4:0) \+((x&amp;0x0000F000LU)?8:0) \+((x&amp;0x000F0000LU)?16:0) \+((x&amp;0x00F00000LU)?32:0) \+((x&amp;0x0F000000LU)?64:0) \+((x&amp;0xF0000000LU)?128:0)&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-If we take part of the first line:(x&amp;0x0000000FLU)It would appear to me that x is being anded with the constant 0x0000000FLU.Its with this constant that I get lost.  To me 0x0000000F = 15 (in decimal).  The &quot;LU&quot; as I underatand it, and I could be wrong, signifies the constant is an Unsigned Long.I understand how the macro is supposed to work.  My problem, I guess is with the syntax.Can someone shed some light?Thanks,Kennykenl@anspach.com</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-74</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Mon, 12 Oct 2009 07:20:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-74</guid>
		<description>MISRA compliance allows exceptions for exactly this kind of thingThis is making your code safer and not less safe:The macros encapsulate individually unsafe constructs but package them up with a very safe interface.You just need a project-wide MISRA deviation specialised to these three macros and you can retain full MISRA compliance, both in the spirit and the letter of the law.Different MISRA checkers handle deviations in different ways but PC-Lint handles macro-specific deviations very nicely, for one example.</description>
		<content:encoded><![CDATA[<p>MISRA compliance allows exceptions for exactly this kind of thingThis is making your code safer and not less safe:The macros encapsulate individually unsafe constructs but package them up with a very safe interface.You just need a project-wide MISRA deviation specialised to these three macros and you can retain full MISRA compliance, both in the spirit and the letter of the law.Different MISRA checkers handle deviations in different ways but PC-Lint handles macro-specific deviations very nicely, for one example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/barr-code/2009/09/binary-literals-in-c/comment-page-1/#comment-73</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Thu, 01 Oct 2009 17:01:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/09/30/binary-literals-in-c/#comment-73</guid>
		<description>Great post (or perhaps steal :-)). I&#039;ve always wanted a way to express binary numbers in C, and now I have one! I took the code and experimented a bit with it. If you turn optimization completely off, them my IAR ARM compiler did include some redundant code. However, the lowest level of optimization fixed that problem.The MISRA compliance checker did of course have an absolute fit over the use of these macros, generating a large number of complaints. I think this is yet another example where one has to make an intelligent trade off. Should I use a macro that is unsafe by MISRA standards, yet allows me to eliminate an entire class of errors?Perhaps in my copious free time I&#039;ll see if I can restructure the macros to make MISRA compliance possible.</description>
		<content:encoded><![CDATA[<p>Great post (or perhaps steal <img src='http://embeddedgurus.com/barr-code/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> ). I&#39;ve always wanted a way to express binary numbers in C, and now I have one! I took the code and experimented a bit with it. If you turn optimization completely off, them my IAR ARM compiler did include some redundant code. However, the lowest level of optimization fixed that problem.The MISRA compliance checker did of course have an absolute fit over the use of these macros, generating a large number of complaints. I think this is yet another example where one has to make an intelligent trade off. Should I use a macro that is unsafe by MISRA standards, yet allows me to eliminate an entire class of errors?Perhaps in my copious free time I&#39;ll see if I can restructure the macros to make MISRA compliance possible.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

