 # Introducing Python Bitwise Operations Examples

by Lewis Loflin

The three files below illustrate how to use modules and functions in Python. This is oriented towards external hardware control with the PC printer port. These don't require use of the port to operate.

The first two files test.py and testa.py do the same thing but import modules and functions from modules in different ways. Look closely and note the differences.

The third file convBinary.py has four functions I wrote to display binary output (string) data in a readable manner on a terminal `under python. The other modules import these functions and use them. This makes reading bits far easier.

I've done this to supply working examples so beginners can get started and avoid the frustrations I had in digging up the information. Feel free to play around with the code.

```#!/usr/bin/env python
# File test.py
# https://bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# This is the same as testa.py except we
# import the entire module convBinary

# This can be done from Idle, Geany, or command line
# Set Geany to use lxterminal

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

# 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.

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

# Here import module:
import convBinary

# rename module
cB = convBinary

# Note that when importing a module or its functions
# one also imports working commands from that module.

# Ex module convBinary has the line "printBits(0x55)
# that will also be executed when this is!

# Open convBinary.py and uncomment those examples
# at the bottom, save, then execute this file and
# note results.

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

# bitwise AND is used for masking out bits:
x = 0x55; y = 0xf0; z = x & y

print "Ex 1 output:"
print
print " X in binary = ", cB.convBinary(x), " =", hex(x), "= dec", x
print " Y in binary = ", cB.convBinary(y), " =", hex(y), "= dec", y
print "                -----------------"
print " Z in binary = ", cB.convBinary(z), " =", hex(z), "= dec", z

# displayed in terminal:

'''
Ex 1 output:

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

'''
print
print

# Instead of the above we can use the function cB.viewNumbers(int, int, int)
# This will produce the same output as above.
print "Ex 1a output:"
print
cB.viewNumbers(x, y, z)

'''
Ex 1a output:

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

'''

print
# import cB member PI and print
print "PI = ", cB.PI()

# how to use commas and the len() function:
print "Length of 'hello' = ", len("hello")

print
print

# Here we use function cB.listBits(int)
print cB.convBinary(0x55)
cB.listBits(0x55)
# Displays:

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

'''

exit

```

```#!/usr/bin/env python
# File testa.py
# https://bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# this is the same as test.py except we import functions
# and not entire convBinary module

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

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

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

print 0b0111, 007, 0x7

# 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.

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

# import 4 specific functions separated by ",":
from convBinary import convBinary, viewNumbers, PI, listBits

# rename imported functions:

cB = convBinary; vN = viewNumbers

# Note that when importing a module or its functions
# one also imports working commands from that module.

# Ex module convBinary has the line "listBits(0x55)
# that will also be executed when this is!

# Open convBinary.py and uncomment those examples
# at the bottom, save, then execute this file and
# note results.

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

# bitwise AND is used for masking out bits:
x = 0x55; y = 0xf0; z = x & y

# Note no more cB.convBinary from before.
# cB, vN, PI, etc are now members of testa.py
print "Ex 1 output:"
print
print " X in binary = ", cB(x), " =", hex(x), "= dec", x
print " Y in binary = ", cB(y), " =", hex(y), "= dec", y
print "                -----------------"
print " Z in binary = ", cB(z), " =", hex(z), "= dec", z

# above four lines does the same thing as vN(int, int)
# or viewNumbers(int, int, int)

# display in terminal:

'''
Ex 1 output:

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

'''
print
print

# Instead of the above we can use the function cB.viewNumbers(int, int, int)
# This will produce the same output as above.

print "Ex 1a output:"
print
# cB.viewNumbers(x, y, z) not needed same output as above
vN(x, y, z)

print
# import cB member PI and print
# print "PI = ", cB.PI() not needed
print "PI = ", PI()

print

# how to use commas and the len() function:
print "Length of 'hello' = ", len("hello")

print
print

# Here we use function cB.listBits(int)
print cB(0x55)
listBits(0x55)
# Displays:

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

'''

exit

```

```#!/usr/bin/env python
# File convBinary.py
# https://bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# Functions used to display numbers in binary on terminal.

# Can Use Geany, Idle, or command line.
# Set Geany to use lxterminal if available.

# Module has 4 functions convBinary,
# viewNumbers, PI, and listBits.

# convert a 16-bit number (integer) to a binary.
# Returns string.

# unlike python bin() this doesn't drop leading zeros

def convBinary(value):
binaryValue = '0b'
for  x in range(0,16):
temp = value & 0x8000
if temp == 0x8000:
binaryValue = binaryValue + '1'
else:
binaryValue = binaryValue + '0'
value = value << 1
return binaryValue

# Displays values x, y, z as binary, HEX, DEC

def viewNumbers(x, y, z):
print " X in binary = ", convBinary(x), " =", hex(x), "= dec", x
print " Y in binary = ", convBinary(y), " =", hex(y), "= dec", y
print "                -----------------"
print " Z in binary = ", convBinary(z), " =", hex(z), "= dec", z
return 0

# This displays bits one at a time as a list

def listBits(value):

for  x in range(0,16):
temp = value & 0x0001
if temp == 0x0001:
print "Bit ", x, " = 1"
else:
print "Bit ", x, " = 0"
value = value >> 1
return 0

def PI():
return 3.1416

# Uncomment below to test
#viewNumbers(0x55, 0x66, 0xff)

'''
X in binary =  0b0000000001010101  = 0x55 = dec 85
Y in binary =  0b0000000001100110  = 0x66 = dec 102
-----------------
Z in binary =  0b0000000011111111  = 0xff = dec 255

'''

# Uncomment to test
#print convBinary(0x55)
#listBits(0x55)

# Displays:

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

'''

exit

```

## Projects

Below are listed a series of projects using pyparallel and electronics. Starting with routines I wrote to aid students I'd advise walking through this in sequence. Have fun and send comments and/or corrections to lewis@bvu.net.