Python Bitwise Operators

OPERATORDESCRIPTIONSYNTAX
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

1. Bitwise AND operator: Returns 1 if both the bits are 1 else 0.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         &
        0100
      = 0000
      = 0 (Decimal)

2. Bitwise or operator: Returns 1 if either of the bit is 1 else 0.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)

3. Bitwise not operator: Returns one’s compliement of the number.

Example:

a = 10 = 1010 (Binary)

~a = ~1010
   = -(1010 + 1)
   = -(1011)
   = -11 (Decimal)

4. Bitwise xor operator: Returns 1 if one of the bit is 1 and other is 0 else returns false.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
# Python program to show 
# bitwise operators 

a = 10
b = 4
	
# Print bitwise AND operation 
print("a & b =", a & b) 
	
# Print bitwise OR operation 
print("a | b =", a | b) 
	
# Print bitwise NOT operation 
print("~a =", ~a) 
	
# print bitwise XOR operation 
print("a ^ b =", a ^ b) 

perm_identity

Python Bitwise Operators

Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as Operand.

Bitwise operators

In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in decimal format.

Note: Python bitwise operators work only on integers.


 

OPERATORDESCRIPTIONSYNTAX
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Let’s understand each operator one by one.

Bitwise AND operator: Returns 1 if both the bits are 1 else 0.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         &
        0100
      = 0000
      = 0 (Decimal)

 
Bitwise or operator: Returns 1 if either of the bit is 1 else 0.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)

 
Bitwise not operator: Returns one’s compliement of the number.

Example:

a = 10 = 1010 (Binary)

~a = ~1010
   = -(1010 + 1)
   = -(1011)
   = -11 (Decimal)

 
Bitwise xor operator: Returns 1 if one of the bit is 1 and other is 0 else returns false.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary

a & b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)

filter_none

edit

play_arrow

brightness_4

# Python program to show

# bitwise operators

  

a = 10

b = 4

    

# Print bitwise AND operation   

print("a & b =", a & b) 

    

# Print bitwise OR operation 

print("a | b =", a | b) 

    

# Print bitwise NOT operation  

print("~a =", ~a) 

    

# print bitwise XOR operation  

print("a ^ b =", a ^ b) 

   

Output:

a & b = 0
a | b = 14
~a = -11
a ^ b = 14

5. Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left as a result. Similar effect as of dividing the number with some power of two.

Example:

Example 1:
a = 10
a >> 1 = 5 

Example 2:
a = -10 
a >> 1 = -5

6. Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids right as a result. Similar effect as of multiplying the number with some power of two.

Example:

a = 5 = 0000 0101
b = -10 = 1111 0110

a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

b << 1 = 0000 1010 = -20
b << 2 = 0001 0100 = -40 
# Python program to show 
# shift operators 

a = 10
b = -10

# print bitwise right shift operator 
print("a >> 1 =", a >> 1) 
print("b >> 1 =", b >> 1) 

a = 5
b = -10

# print bitwise left shift operator 
print("a << 1 =", a << 1) 
print("b << 1 =", b << 1) 

Output:

a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值