
Introducing Python Bitwise Operations Examples
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 # http://www.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 # http://www.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 # http://www.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
Download pport-1.0.iso from Sourceforge.com then burn to DVD (file size 920 meg.), insert into DVD drive and reboot. Make sure PC is set to boot from DVD ROM.
This is pre-configured by myself to use Python to control the printer port. Python can be run from IDLE or Geany.
All of my PPORT electronics projects will work without installation to a PC.
Programs can be saved to thumb drive in LIVE mode.
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.
- Introduction to Python Bitwise Operations
- Python Bitwise Operations by Example
- Using the PC Printer Port series:
- Programming the PC Printer Port in Python
- Additional Commands for Py-Parallel
- Controlling Data Bits on the PC Parallel Port
- Connecting Switches to the PC Printer Port with Python
- Reading an Analog Voltage Through the PC Printer Port Part 1
- Reading an Analog Voltage Through the PC Printer Port Part 2
- Controlling a Serial LCD Display on a PC Printer Port with Python
- Serial ADC and LCD Display with PC Printer Port with Python
- Controlling MAX7219 LED Display with PC Printer Port with Python
- MAX7219 8-Digit LED Display and Serial ADC in Python
- Project pages:
- Part 1: Read Arduino with PC Printer Port
- Part 2: Better way to Read Arduino Through the PC Printer Port
- Part 3: Read-Write an Arduino Through a PC Printer Port
- Part 4: Control LCD Display and Arduino from the PC Printer Port
- Arduino sketches needed by programs:
- pportArduino1.ino read only after reset.
- pportArduino2.ino reset once and multiple reads.
- pportArduino3.ino reset once read write Arduino infinite times - multiple commands.
- Related Raspberry Pi projects:
- Connect Serial LCD to Raspberry Pi
- Serial Read from Arduino to Raspberry Pi
- Arduino Raspberry Pi Interface with LCD Display
Linux Videos
- Live Linux Distro for Using Printer Port with Electronics
- Using the powerful Rox-Filer system in Linux
- Use FEH under Linux for a Wallpaper Setter
- How to create Symbolic links in Linux
- Printer Port Interfacing Videos:
- Connect Electronics to PC printer Port with Python
- Setup PC Printer Port with Python-Linux
- Use PC Printer Port to Read Analog Voltage
- Read-Write Arduino ADC PWN with Printer Port
- Printer Port to Serial LCD Display
- Connect Arduino to PC Printer Port for advanced control
Printer Port in C
- Exploring Digital Computer Electronics
- Hardware Review Connecting PC Parallel Ports
- Operation TB6600 Stepper Controller with PC Parallel Port
- Build or Buy Parallel Port Breakout Board?
- Build Serial HD44780 LCD Display Connect to Parallel Port
- Motherboards
- Presario 1999 CM1001 Gaming Computer Salvage
- Live Test 2002 VIA EPIA-800 Mini ITX Motherboard
- Salvage, Test 2012 AAEON EMB-B75A Industrial Motherboard
- Web Master
- Gen. Electronics
- YouTube Channel
- Arduino Projects
- Raspberry Pi & Linux
- PIC18F2550 in C
- PIC16F628A Assembly
- PICAXE Projects
Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.