Bitwise Operations In Python

I love using Python, and it feels like it was written by technical people for technical people. When dealing with numbers, it just allows…

Photo by Alexander Sinn on Unsplash

Bitwise Operations In Python

I love using Python, and it feels like it was written by technical people for technical people. When dealing with numbers, it just allows you to get right down into bit level. So let’s look at some of the bitwise operations.

Bit operations

In Python, we either have logical operations, such as && for AND and || for OR, or we have bitwise operations, such as & for AND and | for OR. Thus for a bitwise OR operation we have:

A B Z
0 0 0
0 1 1
1 0 1
1 1 1

and for AND:

A B Z
0 0 0
0 1 0
1 0 0
1 1 1

We can then add in a NOT operation, and which will invert a given bit, and an X-OR operation:

A B Z
0 0 0
0 1 1
1 0 1
1 1 0

If we compile a NOT and an OR, we get NOR:

A B Z
0 0 1
0 1 0
1 0 0
1 1 0

And this gives us our basic logic gates, and where we can implement any function we want. The following is the Python code and which operates on eight bit values [here]:

import sys

val1="00110101"
val2="00110111"

if (len(sys.argv)>1):
val1=sys.argv[1]


if (len(sys.argv)>2):
val2=sys.argv[2]

def nor(p, q):
return ~ (p | q) & 0xff;

def nand(p, q):
return ~(p & q) & 0xff;

dec1=int(val1,2)
dec2=int(val2,2)

print ("Decimal form:\t",bin(dec1)[2:10].rjust(8,'0'))
print ("Decimal form:\t",bin(dec2)[2:10].rjust(8,'0'))

print ("\nResult:")
print ("--------------------")
print ("Bitwise AND:\t",bin(dec1 & dec2)[2:10].rjust(8,'0'))
print ("Bitwise NAND:\t",bin(nand(dec1,dec2))[2:10].rjust(8,'0'))
print ("Bitwise OR:\t",bin(dec1 | dec2)[2:10].rjust(8,'0'))

print ("Bitwise NOR:\t",bin(nor(dec1,dec2))[2:10].rjust(8,'0'))

print ("Bitwise XOR:\t",bin(dec1 ^ dec2)[2:10].rjust(8,'0'))

and a sample run is [here]:

Decimal form: 00110101
Decimal form: 00110111
Result:
--------------------
Bitwise AND: 00110101
Bitwise NAND: 11001010
Bitwise OR: 00110111
Bitwise NOR: 11001000
Bitwise XOR: 00000010

Implementing logic with eval()

Python can take this one step forward by using the eval() function, and which will evaluate the result of a logical statement. You can use the bitwise operators (&,|,^ or ~) in a statement. The following implements this [here]:

import sys

operator = "a ^ b"

if (len(sys.argv)>1):
operator=sys.argv[1]

def nor(p, q):
return ~ (p | q) & 0x01

def nand(p, q):
return ~(p & q) & 0x01

def replace(op):
op = op.replace("xor","^")
op = op.replace("and","&")
op = op.replace("or","|")
op = op.replace("not","~")
return(op)

operator = replace(operator)

if ('c' in operator.lower()):
print ("Operator:\t",operator)
print ("A\tB\tC\tZ")
print ("-------------------------")
for a in range(0,2):
for b in range(0,2):
for c in range(0,2):
print (f"{a}\t{b}\t{c}\t{eval(operator) & 0x01}")
else:
print ("Operator:\t",operator)
print ("A\tB\tZ")
print ("-------------------------")
for a in range(0,2):
for b in range(0,2):
print (f"{a}\t{b}\t{eval(operator) & 0x01}")

If we take, a or b and not(c) we have “ a | b & ~( c)”, and it gives the result of [here]:

Operator: a | b & ~(c)
A B C Z
-------------------------
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1

Conclusions

And there you go, bit analysis in Python!