Kotlin NOT Operator
Kotlin NOT Operator !
returns the result of the logical NOT gate operation for the given operand or input value.
Syntax
The syntax to use logical NOT operator is
!x
where x
is a given boolean value, and !
is the symbol used for logical NOT operator.
Return Value
The above expression returns true if x
is false, or false if x
is true.
NOT Truth Table
The output of logic NOT gate operation with an operand x
is given in the following table.
x | !x |
---|---|
true | false |
false | true |
Examples
NOT true is false
In the following program, we take a variable x
initialised with boolean value of true, give as input to logical NOT operator, and find the result.
Kotlin Program
fun main() {
val x = true
val result = !x
println("!x = $result")
}
Output
!x = false
NOT false is true
In the following program, we take a variable x
initialised with boolean value of false, give as input to logical NOT operator, and find the result.
Kotlin Program
fun main() {
val x = false
val result = !x
println("!x = $result")
}
Output
!x = true