Mirror imaging binary numbers

Craig craigtw.online at gmail.com
Wed Dec 6 18:01:17 EST 2006


Matimus wrote:

> 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.

Thanks so much for the response.  I have an array of individual bytes
which will eventually make up a binary bitmap image that is loaded onto
an LCD screen (1 = black dot, 0 = white dot).  At the moment each byte
is reversed to what it should be (completely reverse the bit order):
e.g 00111101 should be 10111100, 11001100 should be 00110011, etc.  It
is not an int problem as such, it is more a bit level swap if you get
what I mean.  If you could help that would be great.




More information about the Python-list mailing list