Get elementary values from a flagged enum
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0; 5 $begingroup$ I created a little function that returns the elementary values from a flagged enum. By "elementary" I mean the values that are powers of 2, excluding any combined enum values. I was a bit surprised I couldn't find a buit-in method for this in .Net (or I missed it). Let's take this flagged enum: [Flags] public enum WeekDay Tuesday Since the binary representation of its values looks as follows... 1 10 100 1000 10000 100000 1000000 1100000 11111 ...I came up with this function to extract the elementary values: public IEnumerable<TEnum> GetElementaryValues<TEnum>() where TEnum: Enum return Enum.GetValues(typeof(TEnum)) .Cast<TEnum>() .Where(v => var intValue = Convert.ToInt64(v); var binary = Convert.ToString(intValue, 2); return !binary.Skip(1).Any(...