473,394 Members | 1,645 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,394 software developers and data experts.

Manipulating bitsets in struct

I'm using Python to do some simple network programming, and found the
struct module very useful for such things, but is there a way to easily
manipulate bitsets such as a 16 bit word being split into 4 parts like 2
bits, 1 bit, 4 bits, and 9 bits?

Perhaps something like:

struct.pack('!h(2:1:4:9)',2,0,1,100)
Brian Vanderburg II

Jul 10 '08 #1
2 1088
Allen wrote:
I'm using Python to do some simple network programming, and found the
struct module very useful for such things, but is there a way to easily
manipulate bitsets such as a 16 bit word being split into 4 parts like 2
bits, 1 bit, 4 bits, and 9 bits?

Perhaps something like:

struct.pack('!h(2:1:4:9)',2,0,1,100)
Brian Vanderburg II


Just 'or' them together after shifting to the left the appropriate number of bits.

-Larry
Jul 10 '08 #2
On Wed, 09 Jul 2008 20:04:24 -0400, Allen wrote:
I'm using Python to do some simple network programming, and found the
struct module very useful for such things, but is there a way to easily
manipulate bitsets such as a 16 bit word being split into 4 parts like 2
bits, 1 bit, 4 bits, and 9 bits?

Perhaps something like:

struct.pack('!h(2:1:4:9)',2,0,1,100)
Take a look at Construct_ instead of `struct`.

... _Construct: http://construct.wikispaces.com/

Ciao,
Marc 'BlackJack' Rintsch
Jul 10 '08 #3

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

Similar topics

7
by: Rajarshi Guha | last post by:
Hi, does Python have any native support for bitsets? I dont seem to see anything in the standard library? Thanks, Rajarshi
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
2
by: Santa | last post by:
Hello: I am trying to manipulating the data from the pointer (the below program) in the function "test1", Is there any best way of changing the data from that pointer (I am changing by assigning...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
0
by: Guy Mahieu | last post by:
Hi, I am trying to dynamically add menu items to external win32 applications from c# code. I have found some VB6 examples on the net that do just that, but when I rewrite them into c# they will...
21
by: felixnielsen | last post by:
If im not mistaken, a char variable allocates 1 byte of memory, as it is the case with a bool variable. I need 2 bool and 1 char variable which only need to contain a value between 0 and 63...
6
by: junky_fellow | last post by:
Hi all, I have a linked list in which each node is of follwoing type. struct node { struct node *fptr; int data; int flag; /* flag could be ORIG=1 or DUPLICATE=2 */ }
3
by: Peeyush81 | last post by:
Hi, I have created a win32 dll with an exported method. extern "C" __declspec(dllexport) void LocateAddress(struct stLocateAddressParam arrAddressParam,int nClientType, LPTSTR outptr); In...
1
by: Sean Farrow | last post by:
Hi: Will the defaul vector < operator allow me to compare two vectors of std:: bitsets? If not how do I get round this? Cheers Sean.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.