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


Boolean operators are the foundation of logic in computer science. They are used to perform logical and bitwise operations on binary data. In Python, the logical not operator is used to negate a boolean expression while the bitwise not operator is used to invert the bits in a number. In this blog post, we will explore the logical and bitwise not operators on booleans in Python.

Operands and Truth Table

The Boolean operators in Python are "and," "or," and "not." The "and" operator gives a result of True if both operands are True and False otherwise. The "or" operator gives a result of True if at least one operand is True and False otherwise. In contrast to the operand's truth value, the "not" operator produces the opposite result.

Here is the truth table for the "and" and "or" operators −

Operand 1

Operand 2

"and"

"or"

True

True

True

True

True

False

False

True

False

True

False

True

False

False

False

False

As for the "not" operator, it has only one operand and its truth table is as follows −

Operand

"not"

True

False

False

True

Algorithm

The logical not operator returns the opposite of a boolean expression. If the expression is true, we return false. If the expression is false, the result is true. The algorithm for the logical not operator is as follows −

  • Evaluate the boolean expression.

  • If the expression is true, return false.

  • If the expression is false, return true.

The bitwise not operator returns the complement of a number. It inverts all the bits in the number, so every 0 becomes 1 and every 1 becomes 0. The algorithm for the bitwise not operator is as follows −

  • Convert the number to binary.

  • Invert all the bits.

  • Convert the resulting binary back to decimal.

Example 1: Logical not Operator

x = True
y = not x
print(y)

Output

False

Example 2: Bitwise not Operator

x = 5  # Decimal 5 is binary 101
y = ~x  # Invert all bits, result is binary -110 (2's complement of 101)
print(y)

Output

-6 

In the first example, we use the logical not operator to negate the boolean value of x. The result is false because x is true. In the second example, we use the bitwise not operator to invert the bits in the number 5. The result is -6 because the inverted binary value (-110) is interpreted as a 2's complement number.

Example 3: Logical and Bitwise not Operators

x = 7  # Decimal 7 is binary 111
y = not x  # Invert boolean value of x, result is false because x is not zero
z = ~x  # Invert all bits in x, result is binary -1000 (2's complement of 111)
w = not(z > 0)  # Is z greater than 0? Invert boolean result, result is true because z is negative
print(y, z, w) 

Output

False -8 True

In this example, we start by assigning the decimal value 7 to the variable x. We then use the logical not operator to invert the boolean value of x. The result is false because x is not zero. Next, we use the bitwise not operator to invert all the bits in x. The result is binary -1000 (2's complement of 111). Finally, we use the logical not operator to invert the boolean result of the comparison z > 0. Since z is negative, the result is true.

Applications

The logical and bitwise not operators can be used in a variety of applications. Here are a few examples −

Boolean algebra − The logical not operator is used in boolean algebra to invert the truth value of a statement. This is useful in logical reasoning and mathematical proofs.

Data manipulation − The bitwise not operator can be used to flip the bits in a binary number. This is useful in data manipulation and encoding.

Security − The bitwise not operator can be used in cryptography to generate a key from a password. By inverting the bits in the password, the resulting key is much harder to guess.

Error checking − The bitwise not operator can be used in error checking algorithms such as the ones used in network protocols. By inverting all the bits in a message, errors can be detected and corrected.

Conclusion

The logical and bitwise not operators are important tools in Python programming. They allow us to invert boolean expressions and flip the bits in binary numbers. These operators can be used in a variety of applications, from boolean algebra to cryptography. By understanding how these operators work, we can write more efficient and secure code.

Updated on: 22-Aug-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements