Last active
March 16, 2019 14:07
-
-
Save skysan87/958c099e2dbafe8a13b9d0cd7a4fcf7f to your computer and use it in GitHub Desktop.
[C#]フラグ(ビット演算とEnum)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BitFlagTest : IFlagTest | |
{ | |
// ビットシフトで表記 | |
const int flag1 = 1 << 0; //2進数:001 10進数:1 16進数:0x1; | |
const int flag2 = 1 << 1; //2進数:010 10進数:2 16進数:0x2; | |
const int flag3 = 1 << 2; //2進数:100 10進数:4 16進数:0x4; | |
int flag = 0; | |
public void AllFlags_On() | |
{ | |
flag = flag1 | flag2 | flag3; | |
} | |
public void Flag1_On() | |
{ | |
// 和集合 | |
// |(OR)演算子 | |
flag |= flag1; | |
} | |
public void Flag2_Off() | |
{ | |
// flag2の補集合の積集合 | |
// &(AND)演算子, ~(反転)演算子 | |
flag &= ~flag2; | |
} | |
public void Switch_Flag3() | |
{ | |
// 排他的論理和 | |
// ^(XOR)演算子 | |
flag ^= flag3; | |
} | |
public void Reverse() | |
{ | |
// 反転 | |
flag = ~flag; | |
} | |
public void ShowFlag() | |
{ | |
//2進数表記 | |
var base2 = Convert.ToString(flag, 2).PadLeft(3, '0'); | |
Console.WriteLine(base2.Substring(base2.Length - 3)); | |
} | |
public bool HasFlag1_or_2() | |
{ | |
return (flag & (flag1 | flag2)) != 0; | |
} | |
public bool HasFlag2_and_3() | |
{ | |
return (flag & (flag2 | flag3)) == (flag2 | flag3); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EnumFlagTest : IFlagTest | |
{ | |
[Flags] | |
enum Hoge | |
{ | |
none = 0, | |
flag1 = 1, | |
flag2 = 2, | |
flag3 = 4, | |
all = flag1 | flag2 | flag3 | |
} | |
Hoge flag = Hoge.none; | |
public void AllFlags_On() | |
{ | |
flag = Hoge.all; | |
} | |
public void Flag1_On() | |
{ | |
flag |= Hoge.flag1; | |
} | |
public void Flag2_Off() | |
{ | |
flag &= ~Hoge.flag2; | |
} | |
public void Switch_Flag3() | |
{ | |
flag ^= Hoge.flag3; | |
} | |
public bool HasFlag1_or_2() | |
{ | |
return flag.HasFlag(Hoge.flag1) || flag.HasFlag(Hoge.flag2); | |
} | |
public bool HasFlag2_and_3() | |
{ | |
return flag.HasFlag(Hoge.flag2) && flag.HasFlag(Hoge.flag3); | |
} | |
public void Reverse() | |
{ | |
//flag = ~flagは負の数になってしまう | |
flag ^= Hoge.all; | |
} | |
public void ShowFlag() | |
{ | |
Console.WriteLine(flag); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IFlagTest | |
{ | |
void AllFlags_On(); | |
void Flag1_On(); | |
void Flag2_Off(); | |
bool HasFlag1_or_2(); | |
bool HasFlag2_and_3(); | |
void Switch_Flag3(); | |
void Reverse(); | |
void ShowFlag(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class Program | |
{ | |
static void Main() | |
{ | |
try | |
{ | |
var list = new List<IFlagTest> | |
{ | |
new BitFlagTest(), | |
new EnumFlagTest() | |
}; | |
foreach (IFlagTest test in list) | |
{ | |
WriteMethodName("Flag1_On"); | |
test.Flag1_On(); | |
test.ShowFlag(); | |
WriteMethodName("AllFlags_On"); | |
test.AllFlags_On(); | |
test.ShowFlag(); | |
WriteMethodName("Flag2_Off"); | |
test.Flag2_Off(); | |
test.ShowFlag(); | |
WriteMethodName("HasFlag1_or_2"); | |
Console.WriteLine(test.HasFlag1_or_2()); | |
WriteMethodName("Reverse"); | |
test.Reverse(); | |
test.ShowFlag(); | |
WriteMethodName("HasFlag2_and_3"); | |
Console.WriteLine(test.HasFlag2_and_3()); | |
WriteMethodName("AllFlags_On"); | |
test.AllFlags_On(); | |
test.ShowFlag(); | |
WriteMethodName("Switch_Flag3"); | |
test.Switch_Flag3(); | |
test.ShowFlag(); | |
WriteMethodName("Switch_Flag3"); | |
test.Switch_Flag3(); | |
test.ShowFlag(); | |
Console.WriteLine(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
static void WriteMethodName(string name) | |
{ | |
Console.Write($"{name + ":",-15}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment