<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Yanesh Tyagi on ASP.Net : VB.Net</title><link>http://weblogs.asp.net/yaneshtyagi/archive/tags/VB.Net/default.aspx</link><description>Tags: VB.Net</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Mystery of Not Operator in VB.Net</title><link>http://weblogs.asp.net/yaneshtyagi/archive/2009/02/03/mystery-of-not-operator-in-vb-net.aspx</link><pubDate>Tue, 03 Feb 2009 08:59:03 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6878758</guid><dc:creator>Yanesh Tyagi</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/yaneshtyagi/rsscomments.aspx?PostID=6878758</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/yaneshtyagi/commentapi.aspx?PostID=6878758</wfw:comment><comments>http://weblogs.asp.net/yaneshtyagi/archive/2009/02/03/mystery-of-not-operator-in-vb-net.aspx#comments</comments><description>&lt;p&gt;(Not 2) = -3. &lt;/p&gt; &lt;p&gt;Strange! &lt;/p&gt; &lt;p&gt;Read below.&lt;/p&gt; &lt;p&gt;In VB.Net, non-zero integer is evaluated as True and an integer whose value is zero is evaluated as False. This is known fact since earliest version of Visual Basic. And this is also supported by VB.Net.&lt;/p&gt; &lt;p&gt;Now consider following code:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Protected Sub &lt;/span&gt;Page_Load(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;sender &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;e &lt;span style="color: blue"&gt;As &lt;/span&gt;System.EventArgs) &lt;span style="color: blue"&gt;Handles Me&lt;/span&gt;.Load
    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;x &lt;span style="color: blue"&gt;As Integer &lt;/span&gt;= 2    &lt;span style="color: green"&gt;'non zero integer is so x = True
    &lt;/span&gt;&lt;span style="color: blue"&gt;If Not &lt;/span&gt;x &lt;span style="color: blue"&gt;Then   &lt;/span&gt;&lt;span style="color: green"&gt;'not True i.e. False
        &lt;/span&gt;Response.Write(&lt;span style="color: #a31515"&gt;"X is False and the value of x is: " &lt;/span&gt;&amp;amp; &lt;span style="color: blue"&gt;Not &lt;/span&gt;x)
    &lt;span style="color: blue"&gt;Else
        &lt;/span&gt;Response.Write(&lt;span style="color: #a31515"&gt;"True"&lt;/span&gt;)
    &lt;span style="color: blue"&gt;End If
End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now there are two questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What will be printed on the page: value of x or True. 
&lt;li&gt;If value of x will be printed, what will be the value of x?&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;When I run the page, It printed &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;X is False and the value of x is: -3&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;which is strange, isn't it? I initialized x with 2. Since this is a non-zero integer, it should be True. So 'Not x' should be evaluated as False in the if condition and else part should be executed. This is supposed to print "True" on the page. But this is not the case.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Mystery of Not&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Investigating the issue, I found that &lt;a href="http://yaneshtyagi.blogspot.com/2008/06/true-false-in-classic-vb.html"&gt;NOT was a bit-wise operator in classical Visual Basic&lt;/a&gt;. It exhibits the same behavior in VB.Net. SO considering the bit-wise calculations,&lt;/p&gt;
&lt;p&gt;2 in Binary is 0000 0010.&lt;/p&gt;
&lt;p&gt;Not 2 will reverse the bit-pattern making it 1111 1101. Considering the last bit is a sign bit, 1111 1101 is -3 in decimal.&lt;/p&gt;
&lt;p&gt;So the if condition now becomes&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;If Not &lt;/span&gt;x &lt;span style="color: blue"&gt;Then  &lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;=&amp;gt; If -3 Then &lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;=&amp;gt; If True &lt;/span&gt;(since -3 is non-zero integer, this is True)&lt;/pre&gt;&lt;pre class="code"&gt;&lt;/pre&gt;
&lt;p&gt;Hence it evaluated the If condition as true and printed the value of x as -3.&lt;/p&gt;
&lt;p&gt;This is strange but true.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://yaneshtyagi.com/Download/Demo/NotInVB.zip"&gt;Download&lt;/a&gt; source code for this post (4KB zip).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fyaneshtyagi%2farchive%2f2009%2f02%2f03%2fmystery-of-not-operator-in-vb-net.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fyaneshtyagi%2farchive%2f2009%2f02%2f03%2fmystery-of-not-operator-in-vb-net.aspx" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6878758" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/yaneshtyagi/archive/tags/Concepts/default.aspx">Concepts</category><category domain="http://weblogs.asp.net/yaneshtyagi/archive/tags/VB.Net/default.aspx">VB.Net</category></item></channel></rss>