Confusing logical operators in VB.NET

I work with an outdated code base written in VB, and came across a conditional statement that I donโ€™t understand, and I canโ€™t figure out what to look for to solve it.

What I mean is the following code and the variables that lead to the truth. The specific parts that I do not understand are (1) the relationship between the first X and the first parens (-2 and (2) the role of X < 2

  • If X is a value below -2 , it evaluates to false .
  • If X is a value above 2 , it evaluates to true .
  • If Y below 5 , it evaluates to true as expected.



 X = 3 Y = 10 If X > (-2 And X < 2) Or Y < 5 Then 'True Else 'False End If 
+2
conditional logical-operators
Sep 20 '17 at 19:44 on
source share
1 answer

I will leave the Or Y < 5 expression for this message uninteresting and limit myself to the X > (-2 And X < 2) side of the expression.

I have not done a lot of VB in the last few years, so I started with some breakthrough in the Operator Priority rules in VB to make sure that everything is fine with me. I found the final information on VBA and VB.Net, as well as some MSDN stuff that might have been VB6, but could also have been the 2013 version of VB.Net. However, they all gave the comparison operators < and > a higher priority over the And operator, regardless of whether you see And as a logical or bitwise one.

With this information, and also knowing that we must first look in parentheses, now I am sure that the first part of the expression to be evaluated will be X < 2 (and not -2 And X ). In addition, we know that this will lead to a logical result, and this logical result must then be converted to an integer in order to execute bitwise ( non- logical) And with -2 . This result (I will call it n ), which is still integer, can be at least comparable to see if X > n , which will give the final result of the expression as Boolean.

I did a few more digging and found this answer about converting VB Booleans to Integer. While the final documentation, I once had the privilege of meeting the author (Hi @JaredPar) and I know that he worked on the VB compiler team at Microsoft, so he should know what he is talking about. This indicates that VB Boolean True has an amazing value of -1 as an integer! False becomes over-normal 0 .

At this point, we need to talk about the binary representation of negative numbers. Using this link as a guide (I vaguely remember learning about it in college, but this is not what I need every day), I am going to provide a conversion table for integers from -3 to +3 in an imaginary integer size of only 4 bits (short version: invert the bit pattern and add it to get a negative representation):

 -3 1101
 -2 1110
 -1 1111 --this helps explain ** why ** -1 was used for True
  0 0000
  1 0001
  2,0010
  3 0010

Stepping back, we consider the original -2 And X < 2 in brackets and look at the results from the possible results (1) and False (0) for X < 2 after the bitwise And with -2 :

 -2 (1110) And True (1111) = 1110 = -2
 -2 (1110) And False (0000) = 0000 = 0 

So, if X < 2 you get True , which leads to -2 ; otherwise you get 0 . It is interesting to note that if our language used positive for True , you would get the same value of 0000 , performing bitwise And with -2 , which you get from False ( 1110 And 0001 ).

Now we know enough to look at some values โ€‹โ€‹for X and determine the result of the entire original expression. Any positive integer is greater than -2 and 0 , so the expression should result in True . Zero and -1 are similar: they are less than two, and therefore will be compared again only as greater than -2, and therefore always lead to True . Negative two, although all lower, should be False .

Unfortunately, this means that you can simplify the whole expression to X > -2 . Either I am mistaken regarding the priority of the operator, my reference to negative integer bit patterns is incorrect, you are using a version of VB that converts True to something other than -1 , or this code is just a get-go super-task.

+5
Sep 20 '17 at 21:00
source share



All Articles