Most of the languages including C, C++, Java and Python provide the boolean type that can be either set to False or True.
Consider below programs that use Logical Not (or !) operator on boolean.
C++
#include <iostream>
using namespace std;
int main()
{
bool is_it_true = false ;
bool is_it_false = true ;
cout << is_it_true << endl;
cout << !is_it_true << endl;
cout << is_it_false << endl;
cout << !is_it_false << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
boolean a = true , b = false ;
System.out.println(!a);
System.out.println(!b);
}
}
|
Python
a = not True
b = not False
print a
print b
|
C#
using System;
class GFG
{
public static void Main ()
{
bool a = true , b = false ;
Console.WriteLine(!a);
Console.WriteLine(!b);
}
}
|
Javascript
<script>
var a = true , b = false ;
document.write(!a+ "<br/>" );
document.write(!b);
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
The outputs of above programs are as expected, but the outputs following programs may not be as expected if we have not used Bitwise Not (or ~) operator before.
C++
#include <iostream>
using namespace std;
int main()
{
bool a = true , b = false ;
cout << ~a << endl;
cout << ~b << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int a = 1 , b = 0 ;
System.out.println(~a);
System.out.println(~b);
}
}
|
Python
a = True
b = False
print ~a
print ~b
|
Javascript
var a = true , b = false ;
console.log(~a);
console.log(~b);
|
C#
using System;
class Program
{
static void Main( string [] args)
{
bool a = true , b = false ;
Console.WriteLine(~Convert.ToInt32(a));
Console.WriteLine(~Convert.ToInt32(b));
}
}
|
Example:
In Python, the logical not operator is used to invert the truth value of a Boolean expression, returning True if the expression is False, and False if the expression is True.
Here’s an example of the not operator:
Python
a = True
b = not a
print (a)
print (b)
|
The time complexity of these two print statements is O(1)
The auxiliary space complexity is also O(1)
In this example, the variable a is assigned the value True, and the variable b is assigned the inverse of a using the not operator. The output of the print statements shows that a is True, while b is False.
The bitwise not operator, on the other hand, is used to invert the bits of an integer, including its sign bit, which changes the sign of the integer. For example:
Python
a = 5
b = ~a
print (a)
print (b)
|
In this example, the variable a is assigned the value 5, and the variable b is assigned the inverse of a using the bitwise not operator. The output of the print statements shows that a is 5, while b is -6.
Note that the bitwise not operator only works on integers, and not on Boolean values. Also, the bitwise not operator is different from the logical not operator, and should be used with caution.
Conclusion:
“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++ and python do auto promotion of boolean to integer type when an integer operator is applied. But Java doesn’t do it.
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above