03. Logic and Bit Operations

Arindam BaidyaArindam Baidya
1 min read

Operators

and - or keyword

>>> age1 = 24
>>> age2 = 16
>>> if(age1 >= 18 and age2 >= 18):
        print("You both are adults")
    elif(age1 >= 18 or age2 >= 18):
        print("One of you is an adult")
    else:
        print("You both are children")

not keyword

>>> is_hungry = False
>>> if(not is_hungry)
        print("You are not hundry")

Bitwise operator

>>> print(15 & 22)
6

>>> print(15 | 22)
31

>>> print(15 ^ 22)
25

>>> print(~22)
-23

Bit shifting

>>> print(22 >> 1)
11

>>> print(22 >> 2)
5

>>> print(22 << 1)
44

Operators

  • Logical operators and not and or returns boolean values based on the passed values

  • Bitwise operators & | ^ and ~ allows us to manipulate single bits of data, and return 0 or 1 based on the value of the bits that are used

  • Bit shifting can be done with the << and >> operators

References

Kodekloud

0
Subscribe to my newsletter

Read articles from Arindam Baidya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arindam Baidya
Arindam Baidya