Most common C# bitwise operations?

C# Logical Bitwise Operators C# Bitwise Helper Class Bitwise operators in C# Operator bitwise.

I did some more work on these extensions - You can find the code here I wrote some extension methods that extend System. Enum that I use often... I'm not claiming that they are bulletproof, but they have helped... Comments removed... namespace Enum. Extensions { public static class EnumerationExtensions { public static bool Has(this System.

Enum type, T value) { try { return (((int)(object)type & (int)(object)value) == (int)(object)value); } catch { return false; } } public static bool Is(this System. Enum type, T value) { try { return (int)(object)type == (int)(object)value; } catch { return false; } } public static T Add(this System. Enum type, T value) { try { return (T)(object)(((int)(object)type | (int)(object)value)); } catch(Exception ex) { throw new ArgumentException( string.

Format( "Could not append value from enumerated type '{0}'. ", typeof(T). Name ), ex); } } public static T Remove(this System.

Enum type, T value) { try { return (T)(object)(((int)(object)type & ~(int)(object)value)); } catch (Exception ex) { throw new ArgumentException( string. Format( "Could not remove value from enumerated type '{0}'. ", typeof(T).

Name ), ex); } } } } Then they are used like the following SomeType value = SomeType. Grapes; bool isGrapes = value. Is(SomeType.

Grapes); //true bool hasGrapes = value. Has(SomeType. Grapes); //true value = value.

Add(SomeType. Oranges); value = value. Add(SomeType.

Apples); value = value. Remove(SomeType. Grapes); bool hasOranges = value.

Has(SomeType. Oranges); //true bool isApples = value. Is(SomeType.

Apples); //false bool hasGrapes = value. Has(SomeType. Grapes); //false.

3 Absolutely brilliant. :) – Jakob Gade Jul 28 '09 at 9:58 Thanks man - appreciate it! – Hugoware Jul 28 '09 at 18:59 I also found this useful - Any ideas how I can modify it so it works on any underlying type?

– Charlie Salts Feb 14 '10 at 5:10 1 These extensions just made my day, my week, my month, and quite possibly my year. – thaBadDawg Mar 3 '10 at 22:39 Thank you! Everybody: be sure to check out the update Hugoware has linked to.

– Helge Klein Mar 30 at 20:12.

The idiom is to use the bitwise or-equal operator to set bits: flags |= 0x04; To clear a bit, the idiom is to use bitwise and with negation: flags &= ~0x04; Sometimes you have an offset that identifies your bit, and then the idiom is to use these combined with left-shift: flags |= 1.

In . NET 4 you can now write: flags. HasFlag(FlagsEnum.

Bit4).

1 +1 for pointing that out, although FlagsEnum is an ugly name. :) – Jim Schubert Jul 19 at 14:25 @Jim, perhaps. It's just a sample name, as used in the original question, so you're free to change it in your code.

– Drew Noakes Jul 20 at 7:46 4 I know! But ugly names are like IE6 and will probably never go away :( – Jim Schubert Jul 20 at 20:18.

C++ syntax, assuming bit 0 is LSB, assuming flags is unsigned long: Check if Set: flags & (1UL.

To test a bit you would do the following: (assuming flags is a 32 bit number) Test Bit: if((flags & 0x08) == 0x08) (If bit 4 is set then its true) Toggle Back (1 - 0 or 0 - 1): flags = flags ^ 0x08; Reset Bit 4 to Zero: flags = flags & 0xFFFFFF7F.

Drew Note that except in the simplest of cases, the Enum. HasFlag carries a heavy performance penalty in comparison to writing out the code manually. Consider the following code: Flags public enum TestFlags { One = 1, Two = 2, Three = 4, Four = 8, Five = 16, Six = 32, Seven = 64, Eight = 128, Nine = 256, Ten = 512 } class Program { static void Main(string args) { TestFlags f = TestFlags.

Five; /* or any other enum */ bool result = false; Stopwatch s = Stopwatch.StartNew(); for (int I = 0; I WriteLine(s. ElapsedMilliseconds); // *4793 ms* s.Restart(); for (int I = 0; I = 0; } s.Stop(); Console. WriteLine(s.

ElapsedMilliseconds); // *27 ms* Console.ReadLine(); } } Over 10 million iterations, the HasFlags extension method takes a whopping 4793 ms, compared to the 27 ms for the standard bitwise implementation.

1 Whilst certainly interesting and good to point out. You do need to consider the usage. According to this if you aren't performing a couple hundred thousand or more ops you probably aren't even going to notice this.

– Joshua Hayes Oct 27 at 12:28.

C++ operations are: & | ^ ~ (for and, or, xor and not bitwise operations). Also of interest are >> and.

1 Question relates to c#, not c++ – Andy Johnson May 17 at 15:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions