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 10 9286
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.
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.
On 2006-12-06, Craig <cr************@gmail.comwrote:
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)
--
Grant Edwards grante Yow! Is this "BOOZE"?
at
visi.com
Craig wrote:
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.
>>import gmpy # GNU Multi-precision library for Python for i in xrange(16):
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
On Dec 6, 6:01 pm, "Craig" <craigtw.onl...@gmail.comwrote:
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
On 2006-12-06, di******@gmail.com <di******@gmail.comwrote:
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
--
Grant Edwards grante Yow! Now I understand the
at meaning of "THE MOD SQUAD"!
visi.com
On Dec 6, 7:20 pm, Grant Edwards <gra...@visi.comwrote:
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
"Craig" <cr************@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
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
Terry Reedy wrote:
"Craig" <cr************@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
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
Thanks for all your great help guys. They work great.
"Craig" <cr************@gmail.comwrote:
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
>>ans
9987
>>>
and Robert is yer aunty...
- Hendrik This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Elliot Marks |
last post by:
I want to output this data to text and binary files. The binary
file contains the garbage you would expect to see if you try to
read it with a text editor, but below that the output appears as...
|
by: J. Campbell |
last post by:
OK...I'm in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I'm starting to understand why C++ makes...
|
by: Bern |
last post by:
how to specifiy a binary number in c++?
hex numbers are specified by 0x prefix
|
by: Tanuki |
last post by:
Hi All:
I encounter a programming problem recently. I need to read a binary
file. I need to translate the binary data into useful information. I
have the format at hand, like 1st byte = ID,...
|
by: Kelvin |
last post by:
Hi everyone:
when we wanna use hex numbers in C, we usually write something like:
int hex_num = 0x12F9;
but how can I declare a binary number in a similar way by putting some
leading words to...
| |
by: nicolasg |
last post by:
does anyone know a module or something to convert numbers like integer
to binary format ?
for example I want to convert number 7 to 0111 so I can make some
bitwise operations...
Thanks
|
by: virtualadepts |
last post by:
In Ancient time mirrors were held to be sacred, but they only had pools
of water and pieces of metal available to observe their own reflection.
The only way to get a clear image of what you looked...
|
by: CoreyWhite |
last post by:
In Ancient times mirrors were held to be sacred, but they only had
pools of water and pieces of metal available to observe their own
reflection. The only way to get a clear image of what you looked...
|
by: Frinavale |
last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number
This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |