Mirror imaging binary numbers

C

Craig

Hi there,

I'm trying to switch binary numbers around so that the MSB becomes the
LSB etc. Is there an easy way of doing this as I can't seem to find
anything. If you could help that would be great. Thanks and good
luck.


Craig
 
M

Matimus

Craig said:
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.
 
C

Craig

Matimus said:
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.
 
G

Grant Edwards

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.

He's already told you 90% of the answer: use the bit operators
& | ~ ^ >> <<.

Here's the remaining 10% of the answer (done a couple different
ways):

def showbits8(b):
mask = 0x80
while mask:
print "01"[(b & mask) != 0],
mask >>= 1
print

def bitswap8a(b):
r = 0
mask = 0x80
while mask:
r >>= 1
if b & mask:
r |= 0x80
mask >>= 1
return r

def bitswap8b(b):
r = 0
for m1,m2 in ((0x80,0x01),(0x40,0x02),(0x20,0x04),(0x10,0x08),(0x01,0x80),(0x02,0x40),(0x04,0x20),(0x08,0x10)):
if b & m1:
r |= m2
return r


def testit(b):
showbits8(b)
showbits8(bitswap8a(b))
showbits8(bitswap8b(b))
print

testit(0xc1)
testit(0x55)
testit(0xe2)
 
M

mensanator

Craig said:
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.
s = gmpy.digits(i,2) # turn int to a base 2 string
p = '0'*(8-len(s)) + s # pad out leading 0's
r = ''.join(reversed(p)) # reverse it
print p,r # voila


00000000 00000000
00000001 10000000
00000010 01000000
00000011 11000000
00000100 00100000
00000101 10100000
00000110 01100000
00000111 11100000
00001000 00010000
00001001 10010000
00001010 01010000
00001011 11010000
00001100 00110000
00001101 10110000
00001110 01110000
00001111 11110000
 
D

dickinsm

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.

Yet another solution:

def flipbits(x):
"""reverse bits in a byte"""
x1 = x << 4 | x >> 4
x2 = (x1 & 51) << 2 | (x1 & 204) >> 2
return (x2 & 85) << 1 | (x2 & 170) >> 1

The idea is to first swap the two nybbles, then swap bits 0, 1, 5, 6
with 2, 3, 6, 7 respectively,
and finally swap bits 0, 2, 4, 6 with bits 1, 3, 5, 7 respectively.

Mark
 
G

Grant Edwards

Yet another solution:

def flipbits(x):
"""reverse bits in a byte"""
x1 = x << 4 | x >> 4
x2 = (x1 & 51) << 2 | (x1 & 204) >> 2
return (x2 & 85) << 1 | (x2 & 170) >> 1

The idea is to first swap the two nybbles, then swap bits 0,
1, 5, 6 with 2, 3, 6, 7 respectively, and finally swap bits 0,
2, 4, 6 with bits 1, 3, 5, 7 respectively.

It's a little less obtuse if you spell it this way:

def flipbits(x):
"""reverse bits in a byte"""
x1 = x << 4 | x >> 4
x2 = (x1 & 0x33) << 2 | (x1 & 0xcc) >> 2
return (x2 & 0x55) << 1 | (x2 & 0xaa) >> 1
 
D

dickinsm

It's a little less obtuse if you spell it this way:

def flipbits(x):
"""reverse bits in a byte"""
x1 = x << 4 | x >> 4
x2 = (x1 & 0x33) << 2 | (x1 & 0xcc) >> 2
return (x2 & 0x55) << 1 | (x2 & 0xaa) >> 1

Granted. And a little more obtuse this way:

def flipbits(x):
"""reverse bits in a byte"""
x += 255*(x & 15)
x += 15*(x & 816)
x += 3*(x & 5440)
return x >> 7

I apologise---it's the end of a long day and I'm feeling more than a
little contrary.

Mark
 
T

Terry Reedy

Craig said:
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.

Using any of the solutions posted by others, I would first make a 256 byte
string in which each byte was the bit reversed version of its index.

IE, bitrev = "\x00\x80\x40\xC0.....\xFF"

Then your actual image processing is a simple, quick lookup for each byte.

Terry Jan Reedy
 
C

Craig

Terry said:
Using any of the solutions posted by others, I would first make a 256 byte
string in which each byte was the bit reversed version of its index.

IE, bitrev = "\x00\x80\x40\xC0.....\xFF"

Then your actual image processing is a simple, quick lookup for each byte.

Terry Jan Reedy

Thanks for all your great help guys. They work great.
 
H

Hendrik van Rooyen

Hi there,

I'm trying to switch binary numbers around so that the MSB becomes the
LSB etc. Is there an easy way of doing this as I can't seem to find
anything. If you could help that would be great. Thanks and good
luck.

Are these Python ints, or are they "strings of bytes of known length"?
And do you really want to mirror swap the binary bits or just change the
byte sequence from say big to little endian?
- i.e. is MSB most significant bit, or byte?

assuming bit, try this:
num = 12345
q,r = divmod(num,2)
q 6172
r 1
l = [r]
while q:
q,r = divmod(q,2)
l.append(r)

l [1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1]
ans = 0
for x in l:
ans = ans * 2 + x


and Robert is yer aunty...

- Hendrik
 
D

Dennis Lee Bieber

On 6 Dec 2006 13:20:14 -0800, "Craig" <[email protected]>
declaimed the following in comp.lang.python:

I know there have been many follow-ups already, but for some reason
I still want to respond to this "as written"...
I'm trying to switch binary numbers around so that the MSB becomes the
LSB etc. Is there an easy way of doing this as I can't seem to find
anything. If you could help that would be great. Thanks and good
luck.
Do you mean BYTES or BITS. Probably bits, as simply reordering bytes
is trivial...

And, of course, the matter of "binary number"... For argument I
shall assume 32-bit integer...

-=-=-=-=-=-=-

NYBBLES = { "0" : "0", "1" : "8",
"2" : "4", "3" : "C",
"4" : "2", "5" : "A",
"6" : "6", "7" : "E",
"8" : "1", "9" : "9",
"A" : "5", "B" : "D",
"C" : "3", "D" : "B",
"E" : "7", "F" : "F" }


def bitReverse(num):
nstr = "%8.8X" % num
rlst = [NYBBLES[c] for c in nstr]
rlst.reverse()
return int("".join(rlst), 16)

-=-=-=-=-=-=-
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top