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

01 Jun, 2022
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++
Python
C#
Javascript
// 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;}
# A Python program that uses Logical Not or ! on booleana = not Trueb = not Falseprint aprint b# Output: False#         True
// C# program that uses Logical// Not or ! on booleanusing 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
<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>
0
1
1
0

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. 
Python
Java
# A Python program that uses Bitwise Not or ~ on booleana = Trueb = Falseprint ~aprint ~b
C/C++

// C/C++ program that uses Bitwise Not or ~ on boolean
#include <bits/stdc++.h>
using namespace std;
int main()
{
    bool a = true, b = false;
    cout << ~a << endl << ~b;
    return 0;
}

// A Java program that uses Bitwise Not or ~ on booleanimport java.io.*; class GFG{    public static void main (String[] args)    {        boolean a = true, b = false;        System.out.println(~a);        System.out.println(~b);    }}
Output:  
6: error: bad operand type boolean for unary operator '~'
        System.out.println(~a);
                           ^
7: error: bad operand type boolean for unary operator '~'
        System.out.println(~b);
                           ^
2 errors
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 
Rajput-Ji
GauravRajput1
jhav365
GFacts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between YOLO and SSD
How to Install and Configure MongoDB in Ubuntu?
Can C++ reference member be declared without being initialized with declaration?
What all is inherited from parent class in C++?
10 Web Development and Web Design Facts That You Should Know
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe