By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

Manipulating bitsets in struct

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.