# Hobby Electronics Projects & More # Python Bitwise Operations Examples

by Lewis Loflin

This is part of my tutorial on using the PC printer port to control hardware. The examples are applicable to other languages such as C and assembly.

These are working examples of bit operations under Python and runs under Linux. It requires the use of convBinary.py and both can be downloaded and saved. The file is self explanatory.

To use the printer port one must setup a module (modified by me) pyparallel. How to set this up is on my webpage Programming the PC Printer Port in Python

```#!/usr/bin/env python
# File examples.py
# http://www.bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# Requires convBinary.py

# We will use bitwise AND, OR, XOR, compliment,
# left and right shift.
# Exploring Python bitwise operators.
# This uses examples - feel free to change variables
# x, y, and z and study the bit patterns.

# Having a scrolling type terminal program
# such as lxterminal is best.

# Each of these examples can be done from a terminal
# or this module can be run as a whole in Linux
# on the command line: python examples.py

# This can be done from Idle, Geany, or command line
# Set Geany to use lxterminal.
# I prefer and use Geany over Idle.

# These examples work the same way in C and assembly.

# convBinary displays results in binary
from convBinary import viewNumbers, convBinary

################################################

# Python accepts integers as binary, octal, and hex.
# The example below illustrates this and print 7 three times:

print 0b0111, 007, 0x07

# Python has functions to convert integers to binary, octal, and hex:
# Should print 0b111 07 0x7

print bin(7), oct(7), hex(7)

# Notice the leading zeros were dropped with the Python functions.
# I wrote my functions viewNumbers, convBinary to show leading zeros.
# This helps the student to better picture what is really happening.

################################################

print
print

# Playing with bits and bytes.

# bitwise AND is used for masking out (changing to 0) selected bits:

x = 0x55; y = 0xf0; z = x & y
print "Ex 1 --> bitwise AND --> z = x & Y"
viewNumbers(x, y, z)

'''
Result: bits D0-D3 are all cleared to 0s.

X in binary =  0b0000000001010101  = 0x55 = dec 85
Y in binary =  0b0000000011110000  = 0xf0 = dec 240
-----------------
Z in binary =  0b0000000001010000  = 0x50 = dec 80

'''

print
print

x = 0x55; y = 0x01; z = x & y
print "Ex 1a --> bitwise AND --> z = x & Y"
viewNumbers(x, y, z)

'''
Result: all bits 0 except bit 0.

Ex 1a - bitwise AND - z = x & Y
X in binary =  0b0000000001010101  = 0x55 = dec 85
Y in binary =  0b0000000000000001  = 0x01 = dec 1
-----------------
Z in binary =  0b0000000000000001  = 0x01 = dec 1

'''

print
print

# bitwise XOR inverts bits when XORed with 1:

x = 0x55; y = 0x0f; z = x ^ y

print "Ex. 2 --> bitwise XOR --> z = x ^ y"
viewNumbers(x, y, z)

'''
Result: bits D0-D3 are all inverted.

X in binary =  0b0000000001010101  = 0x55 = dec 85
Y in binary =  0b0000000000001111  = 0x0f = dec 15
-----------------
Z in binary =  0b0000000001011010  = 0x5a = dec 90

'''

print
print

# bitwise OR is used for setting bits:

x = 0x55; y = 0x02; z = x | y

print "Ex. 3 --> bitwise OR --> z = x | y"
viewNumbers(x, y, z)

'''
Result: bit D1 was set while the others are unchanged.

X in binary =  0b0000000001010101  = 0x55 = dec 85
Y in binary =  0b0000000000000010  = 0x02 = dec 2
-----------------
Z in binary =  0b0000000001010111  = 0x57 = dec 87

'''

print
print

# The compliment function inverts all bits.
# Subtraction in binary is done by adding the compliment
# (all bits inverted) in the subtrahend plus 1.

x = 15; y = ~15 + 1; z = x + y

print "Ex. 4 --> subtraction by adding compliment +1"
print "x = 15; y = ~15 + 1; z = x + y"
viewNumbers(x, y, z)

'''

Result:

X in binary =  0b0000000000001111  = 0xf = dec 15
Y in binary =  0b1111111111110001  = -0xf = dec -15
-----------------
Z in binary =  0b0000000000000000  = 0x00 = dec 0

Carry indicates zero or positive number.

'''

print
print

# Let's look at some examples of this.
# This is a normal subtraction in Python
x = 15; y = 16; z = x - y

print "Ex 5 --> x = 15; y = 16; z = x - y"
viewNumbers(x, y, z)

'''

Result: notice no carry thus negative -
python keeps track of this.

X in binary =  0b0000000000001111  = 0x0f = dec 15
Y in binary =  0b0000000000010000  = 0x10 = dec 16
-----------------
Z in binary =  0b1111111111111111  = -0x01 = dec -1

No carry is a negative number

'''

print
print

# Another example of subtraction:

x = 15; y = 14; z = x - y

print "Ex. 6 --> x = 15; y = 14; z = x - y"
viewNumbers(x, y, z)

'''

Result: has carry thus positive.
Python keeps track of this.

X in binary =  0b0000000000001111  = 0x0f = dec 15
Y in binary =  0b0000000000001110  = 0x0e = dec 14
-----------------
Z in binary =  0b0000000000000001  = 0x01 = dec 1

Once again carry indicates positive result - but that is hidden.

'''

print
print

# What is really going on at the machine level you don't see?
# We compliment the subtrahend and add 1 then add it all together.

x = 15
y = ~15 + 1 # invert bits add 1 same as -15
z = x + y

# check for carry bit (D16) set (1) is positive or zero,
# if clear (0)  number is negative.
# "z & 0xffff"  removes carry bit for display purposes.

print "Ex. 7 --> subtraction by compliment +1 --> 0 or positive result"
print "x = 15; y = ~15 + 1; z = x + y"
viewNumbers(x, y, z)

'''
Result:

A compliment of a number is simply the number
XORed with 0xffff inverting all the bits.
You will have to check for carry (D16) bit
which Python displays but my program ignores.
65536 is simply the carry bit or D16.

Below is in reality 0 with a set carry bit. Use
z = z & 0xffff  to remove carry bit z will = 0

Ex. 7
X in binary =  0b0000000000001111  = 0x0f = dec 15
Y in binary =  0b1111111111110001  = 0xfff1 = dec 65521
-----------------
Z in binary =  0b0000000000000000  = 0x10000 = dec 65536 = 0

Note bit 16 isn't shown for z.
'''

print
print

# Let's do this now for a negative result.

x = 15
y = ~19 + 1 # compliment add 1 same as -19
z = x + y

print "Ex. 8 subtraction by compliment +1 --> negative result"
print "x = 15; y = ~19 + 1; z = x + y"
viewNumbers(x, y, z)
print
print "If I was to compliment z add 1 I'd get 4."
print "Python tracks the carry bit in machine code and knows the sign."
print "In this case no carry thus negative."
print "~z + 1 = ", ~z + 1

'''
Result:

There was no carry so we know this is a negative number.
Look closely and add the bits.

Ex. 8
X in binary =  0b0000000000001111  = 0x0f = dec 15
Y in binary =  0b1111111111101101  = -0x13 = dec -19
-----------------
Z in binary =  0b1111111111111100  = -0x4 = dec -4

'''

print
print

# Finally we come to left and right shift.
# Note the results of the following:

x = 1; y = x << 1; z = y << 1
print "Ex. 9 --> x = 1; y = x << 1; z = y << 1"
viewNumbers(x, y, z)

'''

Result:

Ex. 9 --> x = 1; y = x << 1; z = y << 1
X in binary =  0b0000000000000001  = 0x1 = dec 1
Y in binary =  0b0000000000000010  = 0x2 = dec 2
Z in binary =  0b0000000000000100  = 0x4 = dec 4

Notice bit 0 was shifted two places left = 4.
In reality we are multiplying by powers of 2.
Using the power function this is pow(2,2)
Shifting once twice is the same as shifting two places or 1 << 2.
'''

print
print

x = pow(2,0); y = pow(2,1); z = pow(2,2)
print "Ex 9a - x = pow(2,0); y = pow(2,1); z = pow(2,2)"
viewNumbers(x, y, z) # results same as ex 9

'''
Result: same as above.

Ex 9a - x = pow(2,0); y = pow(2,1); z = pow(2,2)
X in binary =  0b0000000000000001  = 0x01 = dec 1
Y in binary =  0b0000000000000010  = 0x02 = dec 2
Z in binary =  0b0000000000000100  = 0x04 = dec 4

'''

print
print

# 10a. Each shift is multiplying by powers of 2
print "Left shift:"
x = 1; x = x << 0 # 1 * pow(2,0)
print "x = 1; x << 0 = ", convBinary(x), " = ", x

x = 1; x = x << 1 # 1 * pow(2,1)
print "x = 1; x << 1 = ", convBinary(x), " = ", x

x = 1; x = x << 2 # 1 * pow(2,2)
print "x = 1; x << 2 = ", convBinary(x), " = ", x

x = 1; x = x << 3 # 1 * pow(2,3)
print "x = 1; x << 3 = ", convBinary(x), " = ", x

x = 1; x = x << 4 # 1 * pow(2,4)
print "x = 1; x << 4 = ", convBinary(x), " = ", x

x = 1; x = x << 5 # 1 * pow(2,5)
print "x = 1; x << 5 = ", convBinary(x), " = ", x

x = 1; x = x << 6 # 1 * pow(2,6)
print "x = 1; x << 6 = ", convBinary(x), " = ", x

'''
Left shift:
x = 1; x << 0 =  0b0000000000000001  =  1
x = 1; x << 1 =  0b0000000000000010  =  2
x = 1; x << 2 =  0b0000000000000100  =  4
x = 1; x << 3 =  0b0000000000001000  =  8
x = 1; x << 4 =  0b0000000000010000  =  16
x = 1; x << 5 =  0b0000000000100000  =  32
x = 1; x << 6 =  0b0000000001000000  =  64

'''

print
print

# 10b. Each right shift is the same divide by powers of 2
print "Right shift:"
x = 64; x = x >> 0 # 64 / pow(2,0)
print "x = 64; x >> 0 = ", convBinary(x), " = ", x

x = 64; x = x >> 1 # 64 / pow(2,1)
print "x = 64; x >> 1 = ", convBinary(x), " = ", x

x = 64; x = x >> 2 # 64 / pow(2,2)
print "x = 64; x >> 2 = ", convBinary(x), " = ", x

x = 64; x = x >> 3 # 64 / pow(2,3)
print "x = 64; x >> 3 = ", convBinary(x), " = ", x

x = 64; x = x >> 4 # 64 / pow(2,4)
print "x = 64; x >> 4 = ", convBinary(x), " = ", x

x = 64; x = x >> 5 # 64 / pow(2,5)
print "x = 64; x >> 5 = ", convBinary(x), " = ", x

x = 64; x = x >> 6 # 64 / pow(2,6)
print "x = 64; x >> 6 = ", convBinary(x), " = ", x

'''

Right shift:
x = 64; x >> 0 =  0b0000000001000000  =  64
x = 64; x >> 1 =  0b0000000000100000  =  32
x = 64; x >> 2 =  0b0000000000010000  =  16
x = 64; x >> 3 =  0b0000000000001000  =  8
x = 64; x >> 4 =  0b0000000000000100  =  4
x = 64; x >> 5 =  0b0000000000000010  =  2
x = 64; x >> 6 =  0b0000000000000001  =  1

'''

print
print

# Ex. 11
# Shift function combined with a bitwise AND allow us
# to determine bits status in bytes, integers, etc.
# That's also how serial data is read and transmitted.

def printBits(value):

for  x in range(0,16):
temp = value & 0x0001 # mask all but bit 0
if temp == 0x0001:
print "Bit ", x, " = 1"
else:
print "Bit ", x, " = 0"
value = value >> 1 # shift right repeat test
return 0

x = 31234 # choose any number for x to 65535
print "x = ", x, " = ", hex(x), " = ", convBinary(x)
print
printBits(x)

'''
Result:

x =  31234  =  0x7a02  =  0b0111101000000010

Bit  0  = 0
Bit  1  = 1
Bit  2  = 0
Bit  3  = 0
Bit  4  = 0
Bit  5  = 0
Bit  6  = 0
Bit  7  = 0
Bit  8  = 0
Bit  9  = 1
Bit  10  = 0
Bit  11  = 1
Bit  12  = 1
Bit  13  = 1
Bit  14  = 1
Bit  15  = 0

'''

# This completes this introduction on binary bit operations.

exit
```

New January 2018: