Not Operator (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression.

  
result = Not expression  

result
Required. Any Boolean or numeric expression.

expression
Required. Any Boolean or numeric expression.

For Boolean expressions, the following table illustrates how result is determined.

If expression isThe value of result is
TrueFalse
FalseTrue

For numeric expressions, the Not operator inverts the bit values of any numeric expression and sets the corresponding bit in result according to the following table.

If bit in expression isThe bit in result is
10
01
System_CAPS_ICON_note.jpg Note

Since the logical and bitwise operators have a lower precedence than other arithmetic and relational operators, any bitwise operations should be enclosed in parentheses to ensure accurate execution.

For a Boolean negation, the data type of the result is Boolean. For a bitwise negation, the result data type is the same as that of expression. However, if expression is Decimal, the result is Long.

The Not operator can be overloaded, which means that a class or structure can redefine its behavior when its operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

The following example uses the Not operator to perform logical negation on a Boolean expression. The result is a Boolean value that represents the reverse of the value of the expression.

        Dim a As Integer = 10
        Dim b As Integer = 8
        Dim c As Integer = 6
        Dim firstCheck, secondCheck As Boolean
        firstCheck = Not (a > b)
        secondCheck = Not (b > a)

The preceding example produces results of False and True, respectively.

The following example uses the Not operator to perform logical negation of the individual bits of a numeric expression. The bit in the result pattern is set to the reverse of the corresponding bit in the operand pattern, including the sign bit.

        Dim a As Integer = 10
        Dim b As Integer = 8
        Dim c As Integer = 6
        Dim firstPattern, secondPattern, thirdPattern As Integer
        firstPattern = (Not a)
        secondPattern = (Not b)
        thirdPattern = (Not c)

The preceding example produces results of –11, –9, and –7, respectively.

Logical/Bitwise Operators (Visual Basic)
Operator Precedence in Visual Basic
Operators Listed by Functionality
Logical and Bitwise Operators in Visual Basic

Show: