[PYTHON] Bit operation

print('Logical sum')
print(0 | 0)
print(0 | 1)
print(1 | 0)
print(1 | 1)

print('Logical AND')
print(0 & 0)
print(0 & 1)
print(1 & 0)
print(1 & 1)

print('Exclusive OR')
print(0 ^ 0)
print(0 ^ 1)
print(1 ^ 0)
print(1 ^ 1)

print('Invert')
print(bin(0))
print(bin(~0))
print(bin(1))
print(bin(~1))

print('shift')
print(bin(1 << 0))
print(bin(1 << 1))
print(bin(1 << 2))
print(bin(1 << 3))

Execution result:

Logical sum
0
1
1
1
Logical AND
0
0
0
1
Exclusive OR
0
1
1
0
Invert
0b0
-0b1
0b1
-0b10
shift
0b1
0b10
0b100
0b1000

Recommended Posts

Bit operation
Logical operation / bit operation 0
Bit mask
Quotient (truncation) / remainder calculation by bit operation
Bit operation of ring buffer (high speed)
[python] vector operation
Array operation etc. 1
Python OS operation
[Python] Matrix operation
Pandas operation memorandum
PYTHON2.7 64bit version
List reverse operation
SparkFun moto: bit I2C operation and use from MicroPython