473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
3 15583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
3
by: James Lee | last post by:
I am doing a simple query like col1, col2, date, col4 from table1. All four colums are of type blob. For date column, I store a string like this: Fri Feb 13 11:01:24 2004 I store records as...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
10
by: OtisUsenet | last post by:
Hello, I am trying to select distinct dates and order them in the reverse chronological order. Although the column type is TIMESTAMP, in this case I want only YYYY, MM, and DD back. I am...
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
3
by: thrill5 | last post by:
I have an xml document such as: <device> <element>first</element> <element>second</element> </device> I am using this as the source for an xslt transform that goes like <xsl:for-each...
13
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before...
2
by: Doug | last post by:
Hi I have a datagridview in a windows C# application and i am allowing a user to select items from the datagridview. I allow the user to copy the selected items to the clipboard and then if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.