Mirror imaging binary numbers

Matimus mccredie at gmail.com
Wed Dec 6 17:52:49 EST 2006


Craig wrote:
> I'm trying to switch binary numbers around so that the MSB becomes the
> LSB etc.

What do you mean 'binary numbers'? They are all binary. If you mean the
int type, they are 32 bits long and there are 16 bits between the MSB
and LSB (Most/Least Significant _Byte_). Do you want to swap the most
significant word with the least significant word? Swap the most
significant nibble with the least significant nibble in a Byte? Or do
you want to completely reverse the bit order?

To swap nibbles in a byte:

reverseVal = (val & 0xf) << 4  |  (val & 0xf0) >> 4

Basicly you are going to need to use the bit operators (|,&, << and >>)
to get what you need. If you could be more specific perhaps I could be
of more help.




More information about the Python-list mailing list