Saturday, December 28, 2024
Google search engine
HomeLanguagesG-Fact 19 (Logical and Bitwise Not Operators on Boolean)

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

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++




// 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
    // (0) for 'false'
    bool is_it_true = false;
    bool is_it_false = true;
 
    // This code below print first false (0) and then true
    // (1) cause of we have used 'not' (!) operator in second
    // case
    cout << is_it_true << endl;
    cout << !is_it_true << endl;
 
    // This code below print first true (1) and then false
    // (0) cause of we have used 'not' (!) operator in second
    // case
    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);
       
      // Output: false
     //          true
       
    }
}
 
// This code is contributed by aaprilq6i


Python




# A Python program that uses Logical Not or ! on boolean
a = not True
b = not False
print a
print b
# Output: False
#         True


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);
    }
}
// Output: False
//         True
 
// This code is contributed
// by Rajput-Ji


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) 

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




// 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


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.


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));
    }
}


Output

-2
-1

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


Output

True
False

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


Output

5
-6

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 Lazyroar 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 Lazyroar 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
 

RELATED ARTICLES

Most Popular

Recent Comments