Open In App

G-Fact 19 (Logical and Bitwise Not Operators on Boolean)

Improve
Improve
Like Article
Like
Save
Share
Report

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)  # True
print(b)  # False


Output

True
False

Time Complexity: O(1)
Space Complexity: O(1)

Consider below programs that use the Logical Not (or !) operator on a boolean.

C++




// A C++ program that uses Logical Not or ! on boolean
#include <iostream>
using namespace std;
 
int main()
{
    // we are making variable store bool expression of false
    // and true, it can be also written as (1) for 'true' and
    bool is_it_true = false;
    bool is_it_false = true;
 
    // This code below print first false (0) and then true
    cout << is_it_true << endl;
    cout << !is_it_true << endl;
 
    // This code below print first true (1) and then false
    cout << is_it_false << endl;
    cout << !is_it_false << endl;
    return 0;
}


Java




// A Java program that uses Logical Not or ! on boolean
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 Python program that uses Logical Not or ! on boolean
a = not True
b = not False
print a
print b


C#




// C# program that uses Logical
// Not or ! on boolean
using System;
 
class GFG
{
    public static void Main ()
    {
        bool a = true, b = false;
        Console.WriteLine(!a);
        Console.WriteLine(!b);
    }
}


Javascript




<script>
// A javascript program that uses Logical Not or ! on boolean
     
        var a = true, b = false;
        document.write(!a+"<br/>");
        document.write(!b);
 
// Output: False
// True
 
// This code contributed by gauravrajput1
</script>


Output

0
1
1
0

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




// A Java program that uses Bitwise Not or ~ on boolean
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 Python program that uses Bitwise Not or ~ on boolean
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




// A JavaScript program that uses Bitwise Not or ~ on boolean
        var a = true, b = false;
        console.log(~a);
        console.log(~b);
 
// This code is contributed by aaprilq6i.


Output

-2
-1

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)  # 5
print(b)  # -6


Output

5
-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.



Last Updated : 28 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads