Partilhar via


Not Operator (Visual Basic)

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

result = Not expression

Parts

  • result
    Required. Any Boolean or numeric expression.

  • expression
    Required. Any Boolean or numeric expression.

Remarks

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

If expression is

The value of result is

True

False

False

True

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 is

The bit in result is

1

0

0

1

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.

Data Types

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.

Overloading

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 (Visual Basic).

Example

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.

See Also

Reference

Logical/Bitwise Operators (Visual Basic)

Operator Precedence in Visual Basic

Operators Listed by Functionality (Visual Basic)

Concepts

Logical and Bitwise Operators in Visual Basic