Page 1 of 1
BIT order LSB to MSB and vice versa
Posted: Sun Mar 18, 2012 12:06 pm
by Dirk Bubley
Hello together,
i try to find my way with BIT manipulation inside a byte.
One problem i can`t solve is changing the bit order inside a byte.
Example i would like to change the order from LSB------MSB to MSB------LSB or vice versa.
Any help would be aprechiated.
BR
Dirk
Re: BIT order LSB to MSB and vice versa
Posted: Sun Mar 18, 2012 3:43 pm
by medelec35
Hi Dirk,
Not sure if this is a helpful for you or not,
but here are could be couple of solutions?
1st Is from a V5 flowchart.
Since you have not got V5 here is a screen shot:
You could also go to:
http://www.matrixmultimedia.com/mmforum ... 891#p32952
Where you will find a post by Spanish_dude
Martin
Re: BIT order LSB to MSB and vice versa
Posted: Sun Mar 18, 2012 9:23 pm
by JonnyW
The link Martin posts to will work great if you are streaming your data (highest bit first).
If you want to invert a byte and store the new value you can do this in 1 calculation icon:
Code: Select all
byte = (byte >> 7) | (byte >> 5) | (byte >> 3) | (byte >> 1) | (byte << 1) | (byte << 3) | (byte << 5) | (byte << 7)
This is then the inverted byte. You could put this call in a macro for ease of use (argument is byte, return is inverted byte). If you upgrade to v5, then you can use this macro call direct (for neatness) in decision icons, calculations, etc:
I hope this is useful.
Jonny
Re: BIT order LSB to MSB and vice versa
Posted: Sun Mar 18, 2012 11:19 pm
by Dirk Bubley
Hi Jonny,
your bit shift idea is looking good to me. Is this working in simulation too.
Because i feed the LCD Display with the new modified byte and i get strange results.
BR Dirk
Re: BIT order LSB to MSB and vice versa
Posted: Sun Mar 18, 2012 11:50 pm
by JonnyW
Ah, sorry! That's what I get for posting after my bedtime. That will not work at all, forget that code.
Code: Select all
byte = (byte >> 7) & 1 | (byte >> 5) & 2 | (byte >> 3) & 4 | (byte >> 1) & 8 | (byte << 1) & 0x10 | (byte << 3) & 0x20 | (byte << 5) & 0x40| (byte << 7) & 0x80
The code I posted would have 'smeared' the bits across the byte, instead of shifting them on their own. The '&' will isolate each bit correctly.
Jonny
Re: BIT order LSB to MSB and vice versa
Posted: Mon Mar 19, 2012 12:03 am
by Dirk Bubley
Hi Jonny,
perfect it is working that solved my problem!! Many thanks!!
BR
Dirk