By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

best way to reverse bit order of data type ?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.