Casting a boolean to an integer returns -1 for true?

前端 未结 9 780
醉梦人生
醉梦人生 2020-12-01 05:30

I am working with some VB.NET code that seems to be casting a boolean value to an integer using CInt(myBoolean). The odd thing that is happening is that it retu

相关标签:
9条回答
  • 2020-12-01 05:41

    Many versions of BASIC in the 1970's and 1980's implemented bit-wise arithmetic with their AND and OR operators, and made true conditional expressions evaluate to -1 (i.e. the "all-bits-set" value in the two's complement used for integers). I'm not sure exactly why the decision was made to have true conditional expressions evaluate to an all-bits-set value; being able to use AND to mask an integer against a conditional expression may have been faster than multiplying, but given then internal mechanics of the interpreters the difference would have been slight.

    In any case, the first versions of BASIC that Microsoft produced for the PC followed in that tradition of having true conditionals evaluate to -1 (all-bits-set); since QuickBASIC was in turn supposed to be compatible with those, and Visual Basic was supposed to be compatible with QuickBASIC, they used the same representation. Although .Net recognizes integers and Booleans as different types, VB.Net wanted to offer a migration path for VB6 programs that might rely on the old behavior. With "Option Strict Off", VB.Net will implicitly convert a Boolean value of True to an integer -1; while most programmers use Option Strict On, it would be confusing to have the behavior of CInt() differ from the implicit conversion behavior.

    0 讨论(0)
  • 2020-12-01 05:46

    A work around for your initial use would be :

     Dim i As Integer = CInt(Int(False))
    

    This will return a 0.

     Dim i As Integer = CInt(Int(True))
    

    This will return a 1.

    0 讨论(0)
  • 2020-12-01 05:47

    Typically, a value of false is represented by 0 and a value of true is represented by any non-0 integer value. The specific value for true and false (among others) are things that you shouldn't rely on - they can potentially be implementation specific. I'm not sure what you are trying to do, but it would probably be best to not rely on True or False having any specific integer values unless you absolutely have to.

    The best explanation that I could find for VB's specific behavior comes from Wikipedia:

    Boolean constant True has numeric value −1. This is because the Boolean data type is stored as a 16-bit signed integer. In this construct −1 evaluates to 16 binary 1s (the Boolean value True), and 0 as 16 0s (the Boolean value False). This is apparent when performing a Not operation on a 16 bit signed integer value 0 which will return the integer value −1, in other words True = Not False. This inherent functionality becomes especially useful when performing logical operations on the individual bits of an integer such as And, Or, Xor and Not.[4] This definition of True is also consistent with BASIC since the early 1970s Microsoft BASIC implementation and is also related to the characteristics of CPU instructions at the time.

    0 讨论(0)
  • 2020-12-01 05:47

    I have been having the same problem with MySQL as this has no Boolean type only a tinyint(1).

    My solution was to write a converter function to ensure that the values are correct before inserting them into the database

            Public Function BoolToMySql(bVal As Boolean) As Integer
                Dim retVal As Integer
                If bVal = True Then
                    retVal = 1
                Else
                    retVal = 0
                End If
                     BoolToMySql = retVal
            End Function
    
    0 讨论(0)
  • 2020-12-01 05:51

    I hope that is can help others work with Booleans inside VB.NET. Just as a better way to write the VB.NET that Roger wrote:

    Public Function BoolToMySql(bVal As Boolean) As Integer
       return  If(bVal, 1, 0)
    End Function
    
    0 讨论(0)
  • 2020-12-01 05:55

    I had the same problem and used Math.Abs function on the result :)

    0 讨论(0)
提交回复
热议问题