Logical and Bitwise NOT Operators on Boolean in Python

Python | Logical and Bitwise Not Operators: Here, we are going to learn how logical NOT (not) and Bitwise NOT (~) operators work with Boolean values in Python?
Submitted by IncludeHelp, on May 30, 2020

In python, not is used for Logical NOT operator, and ~ is used for Bitwise NOT. Here, we will see their usages and implementation in Python.

Logical NOT (not) operator is used to reverse the result, it returns "False" if the result is "True"; "True", otherwise.

Bitwise NOT (~) operator is used to invert all the bits i.e. it returns the one's complement of the number.

Python program of Logical NOT (not) operator

# Logical NOT (not) operator

x = True
y = False

# printing the values
print("x: ", x)
print("y: ", y)

# 'not' operations
print("not x: ", not x)
print("not y: ", not y)

Output:

x:  True
y:  False
not x:  False
not y:  True

Python program of Bitwise NOT (~) operator

# Bitwise NOT (~) operator

x = True
y = False

# printing the values
print("x: ", x)
print("y: ", y)

# '~' operations
print("~ x: ", ~ x)
print("~ y: ", ~ y)

# assigning numbers
x = 123
y = 128

# printing the values
print("x: ", x)
print("y: ", y)

# '~' operations
print("~ x: ", ~ x)
print("~ y: ", ~ y)

Output:

x:  True
y:  False
~ x:  -2
~ y:  -1
x:  123
y:  128
~ x:  -124
~ y:  -129





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.