enum과 비트마스크
Unity3D/C# 2019. 3. 26. 18:47반응형
enum과 비트마스크
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_03 { class App { public enum eNames { None = 0, Susan = 1, Bob = 2, Karen = 4, All = ~None } //생성자 public App() { var names = App.eNames.Bob | App.eNames.Karen; Console.WriteLine($"names: {names}"); var includeSusan = (names & App.eNames.Susan) != App.eNames.None; Console.WriteLine($"{includeSusan}"); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_03 { class GameEnums { //진영 public enum eCamp { None = 0, Alliance = 100, Horde = 200 } //종족 [Flags] public enum eTribe { None = 0, Human = 1, Dwarf = 2, NightElf = 4, All = ~None } //직업 [Flags] public enum eClasses { None = 0, DeathKnight = 1, Hunter = 2, Rogue = 4, Monk = 8, Mage = 16, Warlock = 32, Priest = 64, Paladin = 128, Warrior = 512, Shaman = 1024, DemonHunter = 2048, Druid = 4096, All = ~None } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study_03 { class Program { static void Main(string[] args) { GameEnums.eClasses humanAvailableClass = GameEnums.eClasses.DemonHunter | GameEnums.eClasses.DeathKnight | GameEnums.eClasses.Druid | GameEnums.eClasses.Rogue | GameEnums.eClasses.Hunter | GameEnums.eClasses.Monk | GameEnums.eClasses.Mage | GameEnums.eClasses.Priest | GameEnums.eClasses.Warrior; Console.WriteLine("종족을 선택해주세요. (1: 인간, 2:드워프, 3:나엘"); var arrTribe = Enum.GetValues(typeof(GameEnums.eTribe)); //foreach (var tribe in arrTribe) //{ // Console.WriteLine(tribe); //} var keyChar = Console.ReadKey().KeyChar; int selectedClassNum; Int32.TryParse(keyChar.ToString(), out selectedClassNum); var selectedClassEnum = (GameEnums.eTribe)arrTribe.GetValue(selectedClassNum); // 10진수를 2진수 문자열로 string strBase2_2 = Convert.ToString((int)humanAvailableClass, 2); //Console.WriteLine(strBase2_2); var allClasses = Enum.GetValues(typeof(GameEnums.eClasses)); //foreach (var availableClass in allClasses) //{ // var a = humanAvailableClass & (GameEnums.eClasses)availableClass; // //var b = Convert.ToString((int)a, 2); // var b = a & ~GameEnums.eClasses.None; // Console.WriteLine("--->" + b); //} var a = humanAvailableClass & GameEnums.eClasses.All; //var b = Convert.ToString((int)a, 2); var b = a & ~GameEnums.eClasses.None; var c = b.ToString().Split(','); foreach (var d in c) { Console.WriteLine(d.ToString().Trim()); } Console.ReadKey(); } } } | cs |
반응형
'Unity3D > C#' 카테고리의 다른 글
컬렉션(C#) (0) | 2019.03.28 |
---|---|
2019.03.26 과제 (와우 케릭터 만들기 C#콘솔) (0) | 2019.03.27 |
C# Array Value Type or Reference (0) | 2019.03.12 |
CLR(Common Language Runtime) (0) | 2019.03.12 |
C# Generic (0) | 2019.01.23 |