473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Re: Efficient Bit addressing in Python.

Ross Ridge wrote:
>This is the code I use to convert large bit arrays to byte strings and
back:

import string
import binascii
import array
8<--------------- examples ----------------------
>I don't think you can do anything faster with standard modules, although
it might not be efficient if you're only working with a single byte.
Thanks I was not aware of binascii module this looks powerful.

- Hendrik

Oct 11 '08 #1
1 2148
Ross Ridge wrote:
>I don't think you can do anything faster with standard modules, although
it might not be efficient if you're only working with a single byte.
Hendrik van Rooyen <ma**@microcorp.co.zawrote:
>Thanks I was not aware of binascii module this looks powerful.
Not really. It's just used as part of a trick to quickly convert a byte
string in to a bit string (a byte string with just 0s and 1s).

Unfortunately fromr you other posts you do seem to be working on
a single byte a time, so my technique probably won't be efficient.
You probably want just want to be using constants and bit masking.
Something like:
PAC_EMERGENCY_STOP = 0x01 # bit #0 on output port 0
PAC_OUTPUT_0_1 = 0x02 # replace with names for other functions
PAC_OUTPUT_0_2 = 0x04
PAC_PNEUMATIC_PUSHER = 0x08 # bit #3 on output port 0
...

def gpio_bit_on(port, bit):
r = gpio_in(port)
r |= bit
r = gpio_out(port)

def gpio_bit_off(port, bit):
r = gpio_in(port)
r &= ~bit
r = gpio_out(port)

class pac(object):
def everything_off(self):
gpio_out(PAC_OUTPUT_PORT_0, 0)
gpio_out(PAC_OUTPUT_PORT_1, 0)
gpio_out(PAC_OUTPUT_PORT_2, 0)
...
gpio_out(PAC_OUTPUT_PORT_7, 0)

def emergency_stop(self):
gpio_bit_on(PAC_OUTPUT_PORT_0, PAC_EMERGENCY_STOP)

def pusher_on(self):
gpio_bit_on(PAC_OUTPUT_PORT_0, PAC_PNEUMATIC_PUSHER)

def pusher_off(self):
gpio_bit_off(PAC_OUTPUT_PORT_0, PAC_PNEUMATIC_PUSHER)
Bit twiddling like this is pretty basic.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Oct 12 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jakub Fast | last post by:
Hi, Does anybody know how far you can get nowadays with trying to use Python as the script language for XUL instead of JS? Is it possible (even theoretically) to write full-fledged applications...
0
by: Stefan Lischke | last post by:
Hi, I'm really desperate using code generation(wsdl.exe) from wsdl files for latest WS-Eventing(including WS-Addressing) Specs. I'm writing my diploma about "publish subscribe systems based on...
12
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task...
3
by: Doru-Catalin Togea | last post by:
Hi! I am writing some tests and I need to place calls through the modem. Is there an API for addressing the COM ports on my machine, so that I can issue AT-commands to the modem? If this can...
10
by: noro | last post by:
Is there a more efficient method to find a string in a text file then: f=file('somefile') for line in f: if 'string' in line: print 'FOUND' ? BTW:
0
by: gerritmitchell | last post by:
Hi, I have a situation where I need to send a SOAP message from a receiver through multiple intermediaries and then to an ultimate receiver. The intial sender will tell the intermediary where...
1
by: =?Utf-8?B?dWx0cmFuZXQ=?= | last post by:
We have a client that uses .Net that needs to work against our Java (xfire) based WS. My question is: how can a .Net (C#) WS client be configured to not send WS-Addressing headers? The client in...
5
by: Ross | last post by:
Forgive my newbieness - I want to refer to some variables and indirectly alter them. Not sure if this is as easy in Python as it is in C. Say I have three vars: oats, corn, barley I add them...
5
by: Hendrik van Rooyen | last post by:
Is there a canonical way to address the bits in a structure like an array or string or struct? Or alternatively, is there a good way to combine eight ints that represent bits into one of the...
0
by: Lie Ryan | last post by:
On Fri, 10 Oct 2008 00:30:18 +0200, Hendrik van Rooyen wrote: You'll find that in most cases, using integer or Boolean is enough. There are some edge cases, which requires bit addressing for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.