Bitwise Operation in python

a = 5
b = 4
or_operation_way_1 = a or b
or_operation_way_2 = a | b
print("After bitwise or operation",or_operation_way_1)
print()
print("After bitwise or operation",or_operation_way_2)
'''
  
                      
      a --> 0 0 0 0 0 1 0 1
      b --> 0 0 0 0 0 1 0 0
    -------------------------
a or b  --> 0 0 0 0 0 1 0 1
'''

and_operation_way_1 = a & b
and_operation_way_2 = a and b
print()
print("After bitwise and operation",and_operation_way_1)
print()
print("After bitwise and operation",and_operation_way_2)

'''                   
       a --> 0 0 0 0 0 1 0 1
       b --> 0 0 0 0 0 1 0 0
    -------------------------
a and b  --> 0 0 0 0 0 1 0 0
'''
xor_operation = a ^ b
print()
print("After bitwise xor operation", xor_operation)
'''                   
       a --> 0 0 0 0 0 1 0 1
       b --> 0 0 0 0 0 1 0 0
    -------------------------
a xor b  --> 0 0 0 0 0 0 0 1
'''
leftshift_operation = a << 1
print()
print("After left shift operation, shifting by one bit",
      leftshift_operation)
'''                   
       a --> 0 0 0 0 0 1 0 1
       <--------------------
       a --> 0 0 0 0 1 0 1 0
       
'''

rightshift_operation = a >> 1
print()
print("After right shift operation,shifting by one bit",rightshift_operation)

''' 
      a --> 0 0 0 0 0 1 0 1
      -------------------->
      a --> 0 0 0 0 0 0 1 0
'''

c = 5
print("After Bitwise NOT operation",~c)
Output:-

After bitwise or operation 5

After bitwise or operation 5

After bitwise and operation 4

After bitwise and operation 4

After bitwise xor operation 1

After left shift operation, shifting by one bit 10

After right shift operation,shifting by one bit 2
After Bitwise NOT operation -6

Note:- Tripple single quotes are used to comment multiple lines in python.

Contributed by – Devanshu Kumar

Please share and comment.

Contribute your article – contribute.articles@tech-bloggers.in

1 thought on “Bitwise Operation in python”

  1. Pingback: Operators in Python – Tech-Bloggers

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock