Bit Manipulation routine

This is a repository of bit manipulation routines.


In [49]:
x = 0x05  #00000111

# Print in Hex and Binary
print ("x = 0x{0:x}".format(x))
print ("x = b\'{0:b}'".format(x))

# Create bitmask with bit 4 enabled
y = 1 << 4
print ("y = b\'{0:b}'".format(y))

# Turn on bit 4 of x
z = x | y
print ("z = x | y = b\'{0:b}'".format(z))

# Turn off z bit 4
w = z ^ y
print ("z ^ y = b\'{0:b}'".format(w))

# Multiply x by 2
print ("2*{} = {}".format(x, x << 1))


x = 0x5
x = b'101'
y = b'10000'
z = x | y = b'10101'
z ^ y = b'101'
2*5 = 10

In [ ]: