473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15627
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.co m

"Laszlo Szijarto" <ls*******@nosp am.mrdoc.cc> wrote in message
news:c8******** **@sulawesi-fi.lerc.nasa.go v...
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)By teVar);

MessageBox.Show (this, ByteVar.ToStrin g());
///

--
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.co m

"Konrad L. M. Rudolph" <ko************ @madrat.net> wrote in message
news:uB******** ******@TK2MSFTN GP11.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)By teVar);

MessageBox.Show (this, ByteVar.ToStrin g());
///

--
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
3728
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 ------------------------------------------------------------- PEP: 323
3
2782
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 they come in so the oldest record is at the top of the table. When I select and display, I want to display them in reverse order (newest record at the top) but I can't sort on date with strings like that.
3
3383
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 please help me? TIA. -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
19
13566
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., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
10
2198
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 using the following query, but it's not returning dates back in the reverse chronological order: SELECT DISTINCT
10
5372
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 string as an input and print it in reverse order.(don't use bulit-in/library function). Sample Output Enter any string: i m Kashif
3
4045
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 select="/device/element> This is the <xsl:value-of select="."/> element.
13
2563
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 submit, which determines exactly where to send the form contents. The table of "receipients" could contain in the region of 3,500 recipients but is more likely to contain up to 1,000. Table structure:
2
4402
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 they chose, to past to the notepad application. But the items that are selected seem to be pasted in the reverse order than the user selected them - for example if the user selected items 1 3 5 then
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.