473,385 Members | 1,620 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,385 software developers and data experts.

Invert bits in a UShort

I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type
ushort. I though I would use the complement operator ~ but the compiler
won't buy it. How can I do this?

Thanks,

--
Peter Aitken

Remove the crap from my email address before using.
Nov 17 '05 #1
3 25239

"Peter Aitken" <pa*****@CRAPnc.rr.com> skrev i en meddelelse
news:er**************@TK2MSFTNGP12.phx.gbl...
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type
ushort. I though I would use the complement operator ~ but the compiler
won't buy it. How can I do this?


Is this what you want:
ushort u1 = 55;
ushort u2 = (ushort) ~u1;
Nov 17 '05 #2
This seems to work for me....

public static void Main()
{
UInt16 a = 0x55;
UInt16 b = (ushort) ~a;
Console.WriteLine("A={0:x}, b={1:x}", a, b);
}
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Peter Aitken" <pa*****@CRAPnc.rr.com> wrote in message
news:er**************@TK2MSFTNGP12.phx.gbl...
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type
ushort. I though I would use the complement operator ~ but the compiler
won't buy it. How can I do this?

Thanks,

--
Peter Aitken

Remove the crap from my email address before using.

Nov 17 '05 #3
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...

"Peter Aitken" <pa*****@CRAPnc.rr.com> skrev i en meddelelse
news:er**************@TK2MSFTNGP12.phx.gbl...
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type
ushort. I though I would use the complement operator ~ but the compiler
won't buy it. How can I do this?


Is this what you want:
ushort u1 = 55;
ushort u2 = (ushort) ~u1;


That's it! Thanks.
--
Peter Aitken

Remove the crap from my email address before using.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Des Small | last post by:
Lately I have found myself using a pattern to make new dictionaries quite often, by which I mean twice: def invert(d): nd = {} ).append(key) for k, v in d] return nd def count(l): d = {}
6
by: Marcia Hon | last post by:
Hi, I am writing a P2P client application. As such, I am creating packets that are to be sent between the peers. I would like to know how in C these bits of the packets may be programmed. These...
4
by: JuLiE Dxer | last post by:
Perhaps, I skipped over learning this particular tidbit. I ran across an odd error while trying to add to ushort variables together and assigning it to a 3rd ushort variable. I get this...
4
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
1
by: xlar54 | last post by:
I have some code that ends up looking alot like: ushort x = (ushort)(m + b); whereas both m and b are ushorts also. This is due to the automatic casting of values to int. Some quick...
1
by: mahesh.nimbalkar | last post by:
Hi, I have color as System.Drawing.Color c1 object as background color. Now I would like to get another System.Drawing.Color c2 object which is invert of c1 color to be used as foreground...
0
by: Karthik | last post by:
Hi, I want to record a sound wave from a mic and at the same time invert it and play the inverted wave.My code goes as follows, however nothing is written into the E:\inverted.wav file.Thanks in...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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.