467,147 Members | 1,202 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,147 developers. It's quick & easy.

best way to reverse bit order of data type ?

In C#, wha't the best way to reveser the bit order of a data type and then
convert it back to that datatype?

So, take a byte, reverse bits, convert it back to a byte. I tried to get a
BitArray and then call Array.Reverse(), but how to convert back to byte?

How about flipping an odd number of bits, say a group of 4 bits, then
converting the 4 bits back to an int equivalent?

Thanks you in advance for your help,
Laszlo
Nov 16 '05 #1
  • viewed: 14557
Share:
3 Replies
Laszlo,

You can use the CopyTo method to copy the bits back into an array of
byte, bool, and int, if you wish. However, for other types, this will not
work. For those types, you might have to use the BitArray class to swap the
bits around, and then get a byte array representing the flipped bits. Once
you have that, you can use the BitConverter class to convert the byte array
to whatever type you wish.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Laszlo Szijarto" <ls*******@nospam.mrdoc.cc> wrote in message
news:c8**********@sulawesi-fi.lerc.nasa.gov...
In C#, wha't the best way to reveser the bit order of a data type and then
convert it back to that datatype?

So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray and then call Array.Reverse(), but how to convert back to byte?

How about flipping an odd number of bits, say a group of 4 bits, then
converting the 4 bits back to an int equivalent?

Thanks you in advance for your help,
Laszlo

Nov 16 '05 #2
Laszlo Szijarto wrote:
In C#, wha't the best way to reveser the bit order of a data type and then
convert it back to that datatype?

So, take a byte, reverse bits, convert it back to a byte. I tried to get a
BitArray and then call Array.Reverse(), but how to convert back to byte?


Why the effort? There's much simpler:

\\\
byte ByteVar = 125;
ByteVar = (byte)(~(int)ByteVar);

MessageBox.Show(this, ByteVar.ToString());
///

--
Konrad -
http://madrat.net/
Nov 16 '05 #3
Konrad,

This will get the inverse of the bits in the byte, not a byte with the
bits in reverse order.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Konrad L. M. Rudolph" <ko************@madrat.net> wrote in message
news:uB**************@TK2MSFTNGP11.phx.gbl...
Laszlo Szijarto wrote:
In C#, wha't the best way to reveser the bit order of a data type and then convert it back to that datatype?

So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray and then call Array.Reverse(), but how to convert back to byte?


Why the effort? There's much simpler:

\\\
byte ByteVar = 125;
ByteVar = (byte)(~(int)ByteVar);

MessageBox.Show(this, ByteVar.ToString());
///

--
Konrad -
http://madrat.net/

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
3 posts views Thread by James Lee | last post: by
3 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
10 posts views Thread by aatish19@gmail.com | last post: by
3 posts views Thread by thrill5 | last post: by
13 posts views Thread by G | last post: by
2 posts views Thread by Doug | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.