{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/693","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/693/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/693/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/693/events","html_url":"https://github.com/dotnet/corefx/issues/693","id":56945491,"number":693,"title":"Add Enum.SetFlag and Enum.RemoveFlag to make bitwise flagging easier","user":{"login":"yufeih","id":511355,"avatar_url":"https://avatars.githubusercontent.com/u/511355?v=3","gravatar_id":"","url":"https://api.github.com/users/yufeih","html_url":"https://github.com/yufeih","followers_url":"https://api.github.com/users/yufeih/followers","following_url":"https://api.github.com/users/yufeih/following{/other_user}","gists_url":"https://api.github.com/users/yufeih/gists{/gist_id}","starred_url":"https://api.github.com/users/yufeih/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yufeih/subscriptions","organizations_url":"https://api.github.com/users/yufeih/orgs","repos_url":"https://api.github.com/users/yufeih/repos","events_url":"https://api.github.com/users/yufeih/events{/privacy}","received_events_url":"https://api.github.com/users/yufeih/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/corefx/labels/api%20addition","name":"api addition","color":"207de5"},{"url":"https://api.github.com/repos/dotnet/corefx/labels/System.Runtime","name":"System.Runtime","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-08T09:30:48Z","updated_at":"2015-02-09T14:32:14Z","closed_at":null,"body":"To maintain multiple bool states in a class, instead of declaring many bool fields, an enum flag can be used to compact these states in to a single member to optimize for memory usage.\r\n```c#\r\n[Flags] enum DirtyFlags { X = 1, Y = 1 << 1, Z = 1 << 2 };\r\n\r\nDirtyFlags dirtyFlags = DirtyFlags.X | DirtyFlags.Y | DirtyFlags.Z;\r\n```\r\nIt is simple to test if a flag is set using `Enum.HasFlag(DirtyFlags.X)`, but the scenario is not complete. Without the ability to set and remove a flag, the following code needs to be maintained manually:\r\n```c#\r\n// Setting a flag\r\ndirtyFlags |= DirtyFlags.X;\r\n// Removing a flag, this is harder to remember :(\r\ndirtyFlags &= ^DirtyFlags.X;\r\n```\r\nThe suggestion is to add 2 methods to Enum to help set and clear a flag.\r\n```c#\r\npublic bool HasFlag(Enum flag); // This method already exists\r\npublic bool SetFlag(Enum flag);\r\npublic bool RemoveFlag(Enum flag);\r\n```\r\nThese methods should be aggressively inlined since bitwise manipulations is very likely to be used in a performance sensitive scenario."},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/73517366","html_url":"https://github.com/dotnet/corefx/issues/693#issuecomment-73517366","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/693","id":73517366,"user":{"login":"mikedn","id":9973420,"avatar_url":"https://avatars.githubusercontent.com/u/9973420?v=3","gravatar_id":"","url":"https://api.github.com/users/mikedn","html_url":"https://github.com/mikedn","followers_url":"https://api.github.com/users/mikedn/followers","following_url":"https://api.github.com/users/mikedn/following{/other_user}","gists_url":"https://api.github.com/users/mikedn/gists{/gist_id}","starred_url":"https://api.github.com/users/mikedn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikedn/subscriptions","organizations_url":"https://api.github.com/users/mikedn/orgs","repos_url":"https://api.github.com/users/mikedn/repos","events_url":"https://api.github.com/users/mikedn/events{/privacy}","received_events_url":"https://api.github.com/users/mikedn/received_events","type":"User","site_admin":false},"created_at":"2015-02-09T14:32:14Z","updated_at":"2015-02-09T14:32:14Z","body":">  sbyte  to  ulong  and the IL for the bitwise math would have to be different for each of those types\r\n\r\nAs I explained before, the bitwise operations themselves are fine. The problem is with conversions that are needed in certain cases. That's something that could probably be fixed in the runtime, at least in the specific case of enums."}}