<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Signed bit-fields</title>
	<link>http://taossa.com/index.php/2006/12/28/signed-bit-fields/</link>
	<description>Continued ramblings on software security and code auditing</description>
	<pubDate>Tue, 07 Sep 2010 10:52:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Robert C. Seacord</title>
		<link>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-33</link>
		<pubDate>Sat, 30 Dec 2006 17:52:34 +0000</pubDate>
		<guid>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-33</guid>
					<description>ISO/IEC 14882-2003 Section 4.5 Integral promotions [conv.prom] Paragraph 3 states that:

An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.

According to my notes, however, for C++ on MS Visual Studio 2005 you get the following behavior: unsigned 8-bit field promotes to unsigned.

This may or may not comply with the standard, depending on how you interpret "can be".</description>
		<content:encoded><![CDATA[<p>ISO/IEC 14882-2003 Section 4.5 Integral promotions [conv.prom] Paragraph 3 states that:</p>
<p>An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.</p>
<p>According to my notes, however, for C++ on MS Visual Studio 2005 you get the following behavior: unsigned 8-bit field promotes to unsigned.</p>
<p>This may or may not comply with the standard, depending on how you interpret &#8220;can be&#8221;.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: jm</title>
		<link>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-28</link>
		<pubDate>Fri, 29 Dec 2006 04:39:17 +0000</pubDate>
		<guid>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-28</guid>
					<description>This is interesting:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16376</description>
		<content:encoded><![CDATA[<p>This is interesting:<br />
<a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16376" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/gcc.gnu.org');">http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16376</a>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: jm</title>
		<link>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-25</link>
		<pubDate>Fri, 29 Dec 2006 00:51:41 +0000</pubDate>
		<guid>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-25</guid>
					<description>Oh, interesting! :&gt;

So, looking at the first example snippit, with my copy of gcc, I actually get that it's promoted to a signed integer value, which is what I suspected would happen. Now, with g++ on the same machine, it gets promoted to an unsigned int, which I was not expecting. :&gt;

I need to install VC++ real quick, as this pretty much runs counter to my understanding of C's integral promotions.</description>
		<content:encoded><![CDATA[<p>Oh, interesting! :></p>
<p>So, looking at the first example snippit, with my copy of gcc, I actually get that it&#8217;s promoted to a signed integer value, which is what I suspected would happen. Now, with g++ on the same machine, it gets promoted to an unsigned int, which I was not expecting. :></p>
<p>I need to install VC++ real quick, as this pretty much runs counter to my understanding of C&#8217;s integral promotions.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Robert C. Seacord</title>
		<link>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-22</link>
		<pubDate>Thu, 28 Dec 2006 23:30:38 +0000</pubDate>
		<guid>http://taossa.com/index.php/2006/12/28/signed-bit-fields/#comment-22</guid>
					<description>Oh, it's much worse than this. 8^)&lt;br /&gt;&lt;br /&gt;The C standard does not really define how to handle bit-fields as&lt;br /&gt;rvalues. If you are unfamiliar with rvalues, in the expression E1 = E2;&lt;br /&gt;E1 is an &#34;lvalue&#34; and E2 is an &#34;rvalue&#34;. As a result, implementations&lt;br /&gt;have adopted different philosophies. The philosophy adopted by gcc is&lt;br /&gt;&#34;use the length of the type (e.g., int)&#34; so that the following program:&lt;br /&gt;&lt;br /&gt;struct {&lt;br /&gt;&#160;&#160;&#160; unsigned int a: 8;&lt;br /&gt;} bits = {255};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main(void) {&lt;br /&gt;&lt;br /&gt;&#160;&#160;&#160; printf(&#34;unsigned 8-bit field promotes to %s.\n&#34;,&lt;br /&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (bits.a &#60;&#60; 24) &#60; 0 ? &#34;signed&#34; : &#34;unsigned&#34;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;When compiled using gcc bits.a is interpreted as an unsigned integer value.&lt;br /&gt;&lt;br /&gt;The philosophy adopted by Microsoft Visual Studio is to &#34;use the length &lt;br /&gt;specified in the bit field&#34;, which in this case is 8.&#160; When this eight-bit &lt;br /&gt;value is used as an rvalue, it is subjected to integer promotions.&#160; On &lt;br /&gt;a typical system (such as IA-32) all of these values can be represented as &lt;br /&gt;a signed int so the value is promoted to signed int. &lt;br /&gt;&lt;br /&gt;This means, of course, that whether or not this value is treated as a &lt;br /&gt;signed or unsigned value is implementation dependent.&#160; This is one of many &lt;br /&gt;implementation dependent aspects of bit-fields.&lt;br /&gt;&lt;br /&gt;To find out how your compiler handles this, try running the following code.&#160; The &lt;br /&gt;results from running this under Microsoft Visual Studio 2005 are provided&lt;br /&gt;as comments.&lt;br /&gt;&lt;br /&gt;----------------------------------------&lt;br /&gt;#include &#60;stdio.h&#62;&lt;br /&gt;#if __cplusplus&lt;br /&gt;#define LANG &#34;C++&#34;&lt;br /&gt;#else&lt;br /&gt;#define LANG &#34;C&#34;&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;struct {&lt;br /&gt;&#160;&#160;&#160; unsigned int a: 8;&lt;br /&gt;} bits = {255};&lt;br /&gt;&lt;br /&gt;struct {&lt;br /&gt;&#160;&#160;&#160; unsigned char a;&lt;br /&gt;} bytes = {255};&lt;br /&gt;&lt;br /&gt;unsigned char a = 255;&lt;br /&gt;&lt;br /&gt;int main(void) {&lt;br /&gt;&#160; printf(LANG &#34;, unsigned 8-bit field promotes to %s.\n&#34;,&lt;br /&gt;&#160;&#160;&#160; (bits.a &#60;&#60; 24) &#60; 0 ? &#34;signed&#34; : &#34;unsigned&#34;);&lt;br /&gt;&#160; // C, unsigned 8-bit field promotes to unsigned.&lt;br /&gt;&#160; // C++, unsigned 8-bit field promotes to unsigned.&lt;br /&gt;&#160;&#160; &#160;&lt;br /&gt;&lt;br /&gt;&#160; printf(LANG &#34;, unsigned 8-bit byte promotes to %s.\n&#34;,&lt;br /&gt;&#160;&#160;&#160; (bytes.a &#60;&#60; 24) &#60; 0 ? &#34;signed&#34; : &#34;unsigned&#34;);&lt;br /&gt;&#160; // C, unsigned 8-bit byte promotes to signed.&lt;br /&gt;&#160; // C++, unsigned 8-bit byte promotes to signed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&#160; printf(LANG &#34;, unsigned char variable promotes to %s.\n&#34;,&lt;br /&gt;&#160;&#160;&#160; (a &#60;&#60; 24) &#60; 0 ? &#34;signed&#34; : &#34;unsigned&#34;);&lt;br /&gt;&#160; // C, unsigned char variable promotes to signed.&lt;br /&gt;&#160; // C++, unsigned char variable promotes to signed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&#160; printf(LANG &#34;, unsigned char bitfield promotes to %s.\n&#34;,&lt;br /&gt;&#160;&#160;&#160; (charfield.a &#60;&#60; 24) &#60; 0 ? &#34;signed&#34; : &#34;unsigned&#34;);&lt;br /&gt;&#160; // C, unsigned char bitfield promotes to signed.&lt;br /&gt;&#160; // C++, unsigned char bitfield promotes to signed.&lt;br /&gt;&lt;br /&gt;&#160; return 0;&#160; &lt;br /&gt;}</description>
		<content:encoded><![CDATA[<p>Oh, it&#8217;s much worse than this. 8^)</p>
<p>The C standard does not really define how to handle bit-fields as<br />rvalues. If you are unfamiliar with rvalues, in the expression E1 = E2;<br />E1 is an &quot;lvalue&quot; and E2 is an &quot;rvalue&quot;. As a result, implementations<br />have adopted different philosophies. The philosophy adopted by gcc is<br />&quot;use the length of the type (e.g., int)&quot; so that the following program:</p>
<p>struct {<br />&nbsp;&nbsp;&nbsp; unsigned int a: 8;<br />} bits = {255};</p>
<p>int main(void) {</p>
<p>&nbsp;&nbsp;&nbsp; printf(&quot;unsigned 8-bit field promotes to %s.\n&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (bits.a &lt;&lt; 24) &lt; 0 ? &quot;signed&quot; : &quot;unsigned&quot;);<br />}</p>
<p>When compiled using gcc bits.a is interpreted as an unsigned integer value.</p>
<p>The philosophy adopted by Microsoft Visual Studio is to &quot;use the length <br />specified in the bit field&quot;, which in this case is 8.&nbsp; When this eight-bit <br />value is used as an rvalue, it is subjected to integer promotions.&nbsp; On <br />a typical system (such as IA-32) all of these values can be represented as <br />a signed int so the value is promoted to signed int. </p>
<p>This means, of course, that whether or not this value is treated as a <br />signed or unsigned value is implementation dependent.&nbsp; This is one of many <br />implementation dependent aspects of bit-fields.</p>
<p>To find out how your compiler handles this, try running the following code.&nbsp; The <br />results from running this under Microsoft Visual Studio 2005 are provided<br />as comments.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />#include &lt;stdio.h&gt;<br />#if __cplusplus<br />#define LANG &quot;C++&quot;<br />#else<br />#define LANG &quot;C&quot;<br />#endif</p>
<p>struct {<br />&nbsp;&nbsp;&nbsp; unsigned int a: 8;<br />} bits = {255};</p>
<p>struct {<br />&nbsp;&nbsp;&nbsp; unsigned char a;<br />} bytes = {255};</p>
<p>unsigned char a = 255;</p>
<p>int main(void) {<br />&nbsp; printf(LANG &quot;, unsigned 8-bit field promotes to %s.\n&quot;,<br />&nbsp;&nbsp;&nbsp; (bits.a &lt;&lt; 24) &lt; 0 ? &quot;signed&quot; : &quot;unsigned&quot;);<br />&nbsp; // C, unsigned 8-bit field promotes to unsigned.<br />&nbsp; // C++, unsigned 8-bit field promotes to unsigned.<br />&nbsp;&nbsp; &nbsp;</p>
<p>&nbsp; printf(LANG &quot;, unsigned 8-bit byte promotes to %s.\n&quot;,<br />&nbsp;&nbsp;&nbsp; (bytes.a &lt;&lt; 24) &lt; 0 ? &quot;signed&quot; : &quot;unsigned&quot;);<br />&nbsp; // C, unsigned 8-bit byte promotes to signed.<br />&nbsp; // C++, unsigned 8-bit byte promotes to signed.</p>
<p>&nbsp; printf(LANG &quot;, unsigned char variable promotes to %s.\n&quot;,<br />&nbsp;&nbsp;&nbsp; (a &lt;&lt; 24) &lt; 0 ? &quot;signed&quot; : &quot;unsigned&quot;);<br />&nbsp; // C, unsigned char variable promotes to signed.<br />&nbsp; // C++, unsigned char variable promotes to signed.</p>
<p>&nbsp; printf(LANG &quot;, unsigned char bitfield promotes to %s.\n&quot;,<br />&nbsp;&nbsp;&nbsp; (charfield.a &lt;&lt; 24) &lt; 0 ? &quot;signed&quot; : &quot;unsigned&quot;);<br />&nbsp; // C, unsigned char bitfield promotes to signed.<br />&nbsp; // C++, unsigned char bitfield promotes to signed.</p>
<p>&nbsp; return 0;&nbsp; <br />}
</p>
]]></content:encoded>
				</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.080 seconds -->
