<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx</link><description>Recently, a number of questions have surfaced about the accuracy of the .NET Framework when working with the binary representation of numbers. (For example, see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=295117 .)</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8372396</link><pubDate>Wed, 09 Apr 2008 17:04:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8372396</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;This is an excellent examination of the topic. However, the conclusion that the .NET Framework is handling this situation correctly is flawed, for one simple reason:&lt;/p&gt;
&lt;p&gt;&amp;quot;Because leading zeroes are always dropped from the non-decimal string representations of numeric values...&amp;quot;&lt;/p&gt;
&lt;p&gt;This is wrong!! When representing negative values using binary two's complement, leading zeros are significant, and cannot be dropped. Dropping them changes the value of the number, and therefore the behavior of the Convert.ToString method is wrong. Simply adding a comment to the documentation saying &amp;quot;its wrong on purpose&amp;quot; is not sufficient; the buggy behavior needs to be fixed.&lt;/p&gt;
</description></item><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8372878</link><pubDate>Wed, 09 Apr 2008 20:53:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8372878</guid><dc:creator>Aaron</dc:creator><description>&lt;p&gt;I’m not sure the previous post is correct. If the leading bits are zero’s and they are dropped then when the number is converted back from binary it will just pad it back out to 32 bits with zeros. This would put bit 31 back as a zero and indicate that the number is positive. &lt;/p&gt;
&lt;p&gt;If the original value was negative, then the bit would be a 1 and would not have been trimmed.&lt;/p&gt;
&lt;p&gt;So, this is int.maxvlue&lt;/p&gt;
&lt;p&gt;01111111111111111111111111111111&lt;/p&gt;
&lt;p&gt;This is int.maxvalue with the leading zero missing &lt;/p&gt;
&lt;p&gt;1111111111111111111111111111111&lt;/p&gt;
&lt;p&gt;And then this is what would happen when it is converted by Convert.Toint32()&lt;/p&gt;
&lt;p&gt;01111111111111111111111111111111&lt;/p&gt;
&lt;p&gt;The zero would be “assumed” by the conversion process. Thus the sign would end up correct.&lt;/p&gt;
</description></item><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8373302</link><pubDate>Thu, 10 Apr 2008 00:12:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8373302</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Aaron,&lt;/p&gt;
&lt;p&gt;You are assuming that the string representation will always be converted back into a number with the same storage size. But this article is about what happens when the binary representation is converted into a number with a different storage size.&lt;/p&gt;
&lt;p&gt;Why make the developer jump through all these hoops to correctly convert the binary representation from one size to another, when all you have to do is treat at least one leading zero is significant? Then the binary representation can be counted on to be accurate regardless of how long it is or what the size was of the location where the value was originally stored. In the example from the article, the result of Convert.ToString(number, HEXADECIMAL) would be a string 33 digits long, which would result in an OverflowException, which is exactly what the developer wants.&lt;/p&gt;
</description></item><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8377632</link><pubDate>Fri, 11 Apr 2008 04:31:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8377632</guid><dc:creator>Miral</dc:creator><description>&lt;p&gt;In ConversionLibrary.ConvertToSignedInteger, wouldn't this comparison make more sense:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// Throw if sign flag is positive but number is interpreted as negative.&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if ((!stringValue.Negative) &amp;amp;&amp;amp; (number &amp;lt; 0))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;throw new OverflowException(String.Format(&amp;quot;0x{0} cannot be converted to an Int32.&amp;quot;, stringValue.Value));&lt;/p&gt;
&lt;p&gt;This way you're not relying on the bit pattern (which actually will cause an implicit widening anyway).&lt;/p&gt;
&lt;p&gt;@David,&lt;/p&gt;
&lt;p&gt;The only way that preserving the leading digit will help you is if you have a rule to automatically sign-extend that leading digit whenever converting away from a string. &amp;nbsp;And I guarantee you that doing that will break almost every app in the world, since strings frequently come from user input or external data sources that won't contain a leading sign digit anyway.&lt;/p&gt;
&lt;p&gt;The problem mentioned in this article only exists if you do have a mismatch in sizes between your string generator and consumer; if due care is taken to use the same types then there isn't a problem. &amp;nbsp;Or at least there wouldn't be a problem if VB joined the Real World and got itself some unsigned types.&lt;/p&gt;
</description></item><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8380160</link><pubDate>Fri, 11 Apr 2008 16:21:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8380160</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Miral,&lt;/p&gt;
&lt;p&gt;If there is no leading sign digit, then it isn't a signed number, and a conversion to a signed type is likely to fail anyway. I'm not concerned with that scenario. I am talking about precisely representing a signed number as a string, which can easily be done by treating a leading 0 as significant. If you truncate all of the leading 0s, then you have to carry the original binary length of the number around with the string representation in order to be able to convert it back to a numeric type. Why force developers to take that extra step, when you could just leave a leading 0 and trust that the string representation is always exact, no matter what length it is?&lt;/p&gt;
</description></item><item><title>re: Working with Signed Non-Decimal and Bitwise Values [Ron Petrusha]</title><link>http://blogs.msdn.com/bclteam/archive/2008/04/09/working-with-signed-non-decimal-and-bitwise-values-ron-petrusha.aspx#8382574</link><pubDate>Sat, 12 Apr 2008 01:46:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8382574</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I don't think I understand why the Convert.ToInt32 operation is working correctly when the example bit pattern (from the paragraph before the &amp;quot;Accidental Change of Type&amp;quot; section) implies the value -0.&lt;/p&gt;
&lt;p&gt;Bit #: &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10987654321098765432109876543210&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10000000000000000000000000000000&lt;/p&gt;
&lt;p&gt;It seems like this specific value is clearly the result of an overflow. (Is -0 legal? If so, why?) However, it would be impossible to say that (int.MaxValue + 2) is &amp;nbsp;or isn't -1 during the conversion, so maybe it makes sense that checking for the specific -0 case is a waste of time.&lt;/p&gt;
</description></item></channel></rss>