python and bit shifts and byte order, oh my!

Reid Nichol rnichol_rrc at yahoo.com
Fri Sep 10 12:50:33 EDT 2004


Daniel Dittmar wrote:
>> I played with bit shifts on my PC and tried the same thing on a Mac OS 
>> X machine.  They produced the same results, so I would assume that the 
>> way the bits and how they are interpreted are as in this link:
>>
>> http://www.khakipants.org/archives/2003/03/bitlevel_input_and_output.html
>>
>> At least in memory while the interpreter is running.
> 
> 
> CPUs differ in the way integers are stored in memory. But the shift 
> operators of the CPU are implemented to work on logical integers in 
> registers, not on consecutive bytes in memory.
> 
> Daniel

I'm wondering because the file format I'm trying to write uses 
bit-packing, so I need to be able to write, for example, a 5 bit integer 
to the disk.

I do think regardless of language I'm going to have an unfun time doing 
this.  But, since cross platform is a want approching a need, I'd like 
to use Python.

Would getting a specific bit from the integer be the same or would I 
have to worry about the byte-order?

ie Would:
x & SOMEBIT
be portable?



More information about the Python-list mailing list