|
|
Subject:
VB - Why true evaluates to -1
Category: Computers > Programming Asked by: bdgood-ga List Price: $3.00 |
Posted:
11 Nov 2002 10:49 PST
Expires: 11 Dec 2002 10:49 PST Question ID: 105365 |
In all versions of VB, why does True evaluate to negative 1? |
|
Subject:
Re: VB - Why true evaluates to -1
Answered By: skermit-ga on 11 Nov 2002 11:28 PST Rated: |
Hello, Ari Bixhorn, Project Manager of Visual Basic at Microsoft, mentions coercing the value of True to be -1 as a matter of convention. It's just the way Microsoft decided True would evaluate in VB 1.0 or whatever the first version was. Their convention is causing a bit of headaches in their .NET initiative, because other languages define True to be 1 and False to be anything else other than 1, or False to be 0 and True as anything else. That's just the way those respective languages have always done it. There's no real rhyme or reason. They're just numeric constants used to represent the boolean ideas of yes or no, right or wrong, true or false. He mentions in the latest version of Visual Basic (their .NET flavor) passing a True within the program will send a -1, but between languages it will pass a 1 to play nice with other languages. Ari then states at the bottom of the post: " Language consistency, on the other hand, implies that there is a *right* way and a *wrong* way to declare arrays, coerce the value of True, etc. Neither C++ nor VB are "wrong." They are simply different. " A good programming technique is to never test for 0, or -1, or 1 for that matter, but to use the proper boolean values TRUE or FALSE in your programming language, that way, you can be sure no matter how it's represented internally, your logic will flow as planned. Thank you for your question. Search Strategy: "visual basic" "value +of true" on google groups: http://groups.google.com/groups?q=%22visual+basic%22+%22value+%2Bof+true%22 Additional Links: Confirmation of VB.NET Language Changes by Ari Bixhorn posted to microsoft.public.dotnet.languages.vb: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=u9SvHSrvAHA.2020%40tkmsftngp04&rnum=7&prev=/groups%3Fq%3D%2522visual%2Bbasic%2522%2B%2522value%2B%252Bof%2Btrue%2522%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3Du9SvHSrvAHA.2020%2540tkmsftngp04%26rnum%3D7 Thank you for the opportunity to answer your question, if you require more information, please clarify the question, or if you find this answer satisfactory, please feel free to rate it. Thank you! skermit-ga |
bdgood-ga
rated this answer:
Thank you very much. That is a great answer. |
|
Subject:
Re: VB - Why true evaluates to -1
From: eddie2002-ga on 11 Nov 2002 13:19 PST |
At the bit-level on the x86, negative numbers are encoded using a method called "two's compliment". With this notation, negative one is stored by setting all the bits in the number to '1'. For example, if you have a 16-bit signed integer, it would be stored internally as 16 bits (or 2 bytes). Negative one would therefore be 16 set bits or (1111 1111 1111 1111). The x86 architecture (and most architectures in fact) have a NOT instruction built it which inverts the bits. Thus the patters 0101 would become 1010 when the NOT instruction is executed on them. When storing a boolean value, visual basic uses a byte (8 bits). This byte is set to 256 (1111 1111) when true and 0 (0000 0000) when false. Since all values in VB are signed, the 256 would be treated as negative one in two's compliment. Most languages have two sets of operators: bitwise and logical. If you store true as positive one (0000 0001), and NOT that value, you'd get negative two (1111 1110). Since in VB, false is 0 and all other values evaluate to true, both 1 and -2 would be true, thus there would not be a way to do this: WHILE NOT EOF(1) ... WEND If EOF were false (not at the end of file), the NOT would convert it to TRUE and the loop would execute. If EOF were true (at the end of the file) and TRUE were positive one, the NOT would turn positive one into negative true and the WHILE loop would consider the negative two to be equivilent to TRUE and execute anyway, resulting in a INPUT PAST END OF FILE error if you were reading information from the file in the loop. In C, TRUE is considered positive one by convention. To deal with this fact, C uses "logical operators" to test the true/false-ness of data and "bitwise operators" to manipulate the bits in a number. Negative one, however is the most logical interprettation of TRUE on platforms such as the x96 since use two's compliment notation for storage of signed values. -Robert |
Subject:
Re: VB - Why true evaluates to -1
From: frox-ga on 15 Nov 2002 01:10 PST |
In Basic (not only in VB) logical operators (AND, OR, XOR, NOT etc.) have an somehow ambiguous meaning. They are in effects binary operators, not real logical operators Binary operators perform an operation between each bit of the operands, while logical operators perform operations between numbers in their whole. The problem is that, as Basic does not have a specific type for handling logical values, we are forced to use 16-bit numbers AS if they were logical values. This works nicely if we always work with the pre-defined True and False values, but gets trickier when you work with numbers. For example, in common-sense reasoning you can say that if something is true, it's opposite is false This is true for logical operators but not always true with VB's bit-wise operators. Let's see how the NOT operation works on a number like 5. 5 in binary is 101 ( 0000 0000 0000 0101 in 16-bit representation). Let's invert each bit, we get 1111 1111 1111 1010. In binary two's complement representation that makes 6. If you try in VB: Debug.Print Not 5 That's exactly what you will get. I won't enter into details of why this happens, the important is to understand that it happens. The assumption " if something is true, it's opposite is false" must be therefore be used with caution in VB programming when you use numbers instead of the pre-defined True/False values Try the following snippet of code: a = 5 If a Then If Not a Then MsgBox "What? I should never get into here!" Else MsgBox "All works as we expected" End If End If A is 5, so the test "If a" succeeds, and execution continues. But "Not a" evaluates to 6, not to 0 (the numerical equivalent of False) so the test "If Not a" also succeeds. There is an "If Not a" test within a "If a" test and both succeed!! So, it could seem that in this case a is both true and false. The real fact is that in this case a is neither True neither False (not the difference in capitalisation I am using here!) Another example of odd VB code: a = 5 If a Then MsgBox "A IS TRUE" If Not a Then MsgBox "A IS FALSE" This code first says "A IS TRUE" and then "A IS FALSE". This is just to confuse a little bit the things: all works wonderfully if we only work with values of True and False , or their numerical equivalents 1 and 0. Frox-ga |
Subject:
Re: VB - Why true evaluates to -1
From: frox-ga on 15 Nov 2002 01:12 PST |
In my prevoious post, there was a particularly confusing typo. The phrase (not the difference in capitalisation I am using here!) should be read: (note the difference in capitalisation I am using here!) Sorry for that. frox-ga |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |