Getting at the bits of a 32-bit integer
grante at visi.com
Tue Aug 24 03:25:09 CEST 2004
On 2004-08-24, Jeremy Bowers <jerf at jerf.org> wrote:
>>>> a = 1023
>>>> (a & 1024) >> 10
> Obviously, you need better constants and stuff.
> However, I must challenge your need to do this in the first place.
That's a bit, um, conceited. Somebody's got to twiddle all
those low-level bits out there to provide the infrastructure
that makes the world run.
I'm one of those people, and I need to do bit-twiddling quite
often while implimenting various communications protocols.
> Python and bit twiddling generally don't go together;
Says you. I say they go together quite well. Bit-twiddling in
Python works great. It's a hell of a lot easier than doing it
in C, and I've switched to Python for all my non-embedded
> even if you are accessing a binary file or protocol you should
> shuffle out as much to the struct (?) module as possible.
Struct only works at the byte level. For bitfields within
bytes, you've got to use the bitwise and/or/xor/shift operators
just like you do in C.
> If you want to track every bit and byte, Python is the wrong
> language for you...
So, what language do you think is better? It sure isn't C. I
did bit-twiddling in C for 20 years, and doing it in Python is
> and unless you are tracking many many millions of bits, this
> is the wrong decade for it.
I guess I plain don't understand that last clause. It seems to
imply that if you _are_ tracking many millions of bits, this is
the decade to do it in Python?
I used C for many, many years for low-level stuff like tearing
apart serial data streams, Ethernet frames, IP headers and
suchlike, and I can assure you that it's far, far easier to do
stuff like that in Python than in C.
Grant Edwards grante Yow! Where's SANDY DUNCAN?
More information about the Python-list