bitwise operations in python

python also have bitwise operations. and the operations works with only integer type.

the operations are like below

x | y         bitwise or of x and y
x ^ y        bitwise exclusive or of x and y
x & y        bitwise and of x and y

x << y       x shifted left by y bits
x >> y       x shifted right by y bits
the y value ( right-hand operand ) has to be positive value. otherwise, ValueError will be raised.

~x            the bits of x inverted

if you cannot understand the terms such as "and", "or" and "exclusive", refer to wiki page-logical operation.

let's check the example code.


x = 1
y = 3

print("x | y = {}".format(x | y))
print("x ^ y = {}".format(x ^ y))
print("x & y = {}".format(x & y))
print("x << y = {}".format(x << y))
print("x >> y = {}".format(x >> y))
print("~x = {}".format(~x))


Comments

Popular posts from this blog

making menubar and menu on QML

let's start QML programming with PySide2

QSizePolicy Fixed example in pyside2