G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
Most of the languages including C, C++, Java, and Python provide a boolean type that can be either set to False or True. In this article, we will see about logical and bitwise not operators on boolean.
Logical Not (or !) Operator in Python
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. In the below example, we have used the logical not operator in Python. Here, 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.
Python3
a = True
b = not a
print (a)
print (b)
|
Time Complexity: O(1)
Space Complexity: O(1)
Consider below programs that use the Logical Not (or !) operator on a 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);
}
}
|
Python3
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)
Bitwise Not (or ~) Operator in Python
The bitwise not operator is used to invert the bits of an integer, including its sign bit, which changes the sign of the integer. In this example, we are using Btiwise Not ( or ~) operator. Code implementation is given below.
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
|
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));
}
}
|
Javascript
var a = true , b = false ;
console.log(~a);
console.log(~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.
Python
a = 5
b = ~a
print (a)
print (b)
|
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.
Last Updated :
28 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...