[.net] Bitwise check - best way?

Started by
15 comments, last by devronious 16 years, 2 months ago
Quote:You are only missing one nice feature of .Net, the FlagsAttribute


Super awesome! Didn't know that. I will use that from now on.

Hey, along that subject, what's the difference between [Flags] and [Flags()]? Is there? Or is it just extra typing?

SiCrane,

Holy Crap, how did you figure that out? That's pretty interesting and smart. Thanks. I wonder if a universal one can be made for various different enums? Or if we can somehow custom add our own operator to Visual Studio 2005-2008? Perhaps thru the SDK? Hmmm...

-Devin
Advertisement
If you really want one for all Enums:
  class Contains {    Contains(Int64 d) { v = d; }    Int64 v;    public static bool operator &(Contains c, Enum d) {      return (c.v & Convert.ToInt64(d)) != 0;    }    public static explicit operator Contains(Enum d) {      return new Contains(Convert.ToInt64(d));    }  }

It's still evil, however.
Cool, I also put in a feature request for something to this effect with a keyword.

[edit]

Quote:It's still evil, however


Ya I would tend to think that is a bit around the corner and down the road and across the bridge thru some towns, and so on :) But still you figured it out which gives merit.

[Edited by - devronious on February 10, 2008 8:25:45 PM]
Quote:Original post by SiCrane
If you really want one for all Enums:
  class Contains {    Contains(Int64 d) { v = d; }    Int64 v;    public static bool operator &(Contains c, Enum d) {      return (c.v & Convert.ToInt64(d)) != 0;    }    public static explicit operator Contains(Enum d) {      return new Contains(Convert.ToInt64(d));    }  }

It's still evil, however.


You might go the extra mile and make a C# 3.0 extension method on the Enum base class itself. I don't have my tools installed on this machine yet (building a new computer) but it seems like you should be able to make something less "evil" that way.
I'm not really familiar with C# 3 right now. Could you give an example when you're set up?

Quote:
Cool, I also put in a feature request for something to this effect with a keyword.

Though if you really want something that looks like a keyword:
  public struct Proxy {    internal long v;        public Proxy(long val) {      v = val;    }        public static bool operator >(Proxy p, Enum e) {      return (p.v & Convert.ToInt64(e)) != 0;    }    public static bool operator <(Proxy p, Enum e) {      return (p.v & Convert.ToInt64(e)) != 0;    }  }    struct C {    public static Proxy operator <(Enum e, C c) {      return new Proxy(Convert.ToInt64(e));    }    public static Proxy operator >(Enum e, C c) {      return new Proxy(Convert.ToInt64(e));    }        static C contains = new C();  }  if (this.bufferDataChanged <C.contains> DataType.Vertex) {do this...}  // or  C contains = new C();  if (this.bufferDataChanged <contains> DataType.Vertex) {do this...}

As you may imagine, still evil.
A C#3 example could be something like this:
public static class EnumContains {	public static bool Contains(this Enum e, Enum bit) {		return (Convert.ToInt32(e) & Convert.ToInt32(bit)) != 0;	}}


You could then use it like this:
class Program {	[Flags()] 	public enum DataType {		None = 0,		Vertex = 1,		Index = 2,		UV = 4,		XYZ = 8,		Norm = 16	}	static void Main(string[] args) {				var Test = DataType.UV | DataType.Norm;		Console.WriteLine("Test = {0}", Test);		foreach (DataType Bit in Enum.GetValues(typeof(DataType))) {			Console.WriteLine("Test.Contains({0})\t={1}", Bit, Test.Contains(Bit));		}	}}

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Thanks guys, really interesting to see the different solutions to the same problem. Goes to show how programming is so broad and open.

This topic is closed to new replies.

Advertisement