C#のオペレータ(演算子)

オペレータ(演算子)は、計算式を構成する要素の中で計算や比較を行う為の記号です。
計算式のその他の部分はオペランド(被演算子)と呼ばれます。
1つ以上の値から別の値を生成する事が出来ます。



算術演算子

演算子解説
+単項プラス
Console.WriteLine(+4);           // output: 4
-単項マイナス
Console.WriteLine(-4);           // output: -4
Console.WriteLine(-(-4));        // output: 4
Console.WriteLine(-double.NaN);  // output: NaN
+加算
Console.WriteLine(5 + 4);        // output: 9
Console.WriteLine(5 + 4.3);      // output: 9.3
-減算
Console.WriteLine(47 - 3);      // output: 44
Console.WriteLine(5 - 4.3);     // output: 0.7
*乗算
Console.WriteLine(5 * 2);         // output: 10
Console.WriteLine(0.5 * 2.5);     // output: 1.25
/除算(整数の場合は切り捨て)
Console.WriteLine(13 / 5);        // output: 2
Console.WriteLine(-13 / 5);       // output: -2
Console.WriteLine(13 / 5.0);      // output: 2.6
%剰余
Console.WriteLine(5 % 4);         // output: 1
Console.WriteLine(5 % -4);        // output: 1
Console.WriteLine(-5 % 4);        // output: -1
Console.WriteLine(-5 % -4);       // output: -1
Console.WriteLine(-5.2f % 2.0f);  // output: -1.2
Console.WriteLine(5.9 % 3.1);     // output: 2.8
++インクリメント(1を足す)
int i = 3;
Console.WriteLine(i);             // output: 3
Console.WriteLine(i++);           // output: 3
Console.WriteLine(i);             // output: 4
Console.WriteLine(++i);           // output: 5
--デクリメント(1を引く)
int i = 3;
Console.WriteLine(i);             // output: 3
Console.WriteLine(i--);           // output: 3
Console.WriteLine(i);             // output: 2
Console.WriteLine(--i);           // output: 1

インクリメント・デクリメントの前置と後置

インクリメント(デクリメント)は演算子を前に付ける前置と後に付ける後置があります。
前置の場合、値に1が足された(引かれた)後、メソッドへ値が渡されます。
後置の場合、メソッドへ値が渡された後、値に1が足され(引かれ)ます。



代入演算子

演算子解説
=右辺の値を左辺の変数へ格納する
a = b;     // 
c = d = e; // c = (d = e)と同じ
[演算子]=複合代入(演算を行った後代入する)
※[演算子]は算術、ブール論理、ビットシフト
a += b;    // a = a + bと同じ
c -= d;    // c = c - dと同じ
??=null合体割り当て(左辺の値がnullの時だけ代入される)
// aがnullの時だけ初期化
a ??= new List<int>();



比較演算子

演算子解説
==等値(左辺と右辺が等しい場合true)
int a = 1 + 2 + 3;
int b = 6;
Console.WriteLine(a == b);  // output: True
!=非等値(左辺と右辺が等しくない場合true)
int a = 1 + 1 + 2 + 3;
int b = 6;
Console.WriteLine(a != b);  // output: True
<小なり(左辺が右辺より小さいなら true)
Console.WriteLine(7.0 < 5.1);   // output: False
<=以下(左辺が右辺以下なら true)
Console.WriteLine(7.0 <= 5.1);   // output: False
>大なり(左辺が右辺より大きいなら true)
Console.WriteLine(7.0 > 5.1);   // output: True
>=以上(左辺が右辺以上なら true)
Console.WriteLine(7.0 >= 5.1);   // output: True



論理演算子

ブール型の論理演算子

演算子解説
!否定(右辺がtrueならfalse、falseならtrue)
bool passed = false;
Console.WriteLine(!passed);  // output: True
Console.WriteLine(!true);    // output: False
&論理AND(左辺と右辺の両方trueならtrue、それ以外はfalse)
Console.WriteLine(true & true);    // output: True
Console.WriteLine(true & false);   // output: False
Console.WriteLine(false & true);   // output: False
Console.WriteLine(false & false);  // output: False
|論理OR(左辺と右辺のどちらかがtrueならtrue、それ以外はfalse)
Console.WriteLine(true | true);    // output: True
Console.WriteLine(true | false);   // output: True
Console.WriteLine(false | true);   // output: True
Console.WriteLine(false | false);  // output: False
^排他的OR(左辺と右辺が異なるならtrue、同じならはfalse)
Console.WriteLine(true ^ true);    // output: False
Console.WriteLine(true ^ false);   // output: True
Console.WriteLine(false ^ true);   // output: True
Console.WriteLine(false ^ false);  // output: False


論理演算と条件付き論理演算の違い

論理ANDと条件付き論理ANDは同じように動作するように見えますが明確な違いがあります。
論理演算では左辺と右辺の両方を評価してから結果を返します。
左辺や右辺にメソッドであった場合必ずメソッドが実行されます。

条件付き論理演算では左辺で結果が確定する場合、右辺は評価されません。
右辺がメソッドであった場合メソッドは実行されない事もあります。
bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}
bool a = false & SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True


整数型の論理演算子

演算子解説
&論理AND(ビットごとに両方1なら1、それ以外は0)
uint a = 0b_1111_1000;
uint b = 0b_1001_1101;
uint c = a & b;    // c = 0b_1001_1000;
|論理OR(ビットごとにどちらかが1なら1、それ以外は0)
uint a = 0b_1010_0000;
uint b = 0b_1001_0001;
uint c = a | b;    // c = 0b_1011_0001;
~ビットごとの補数(各ビットを反転)
uint a = 0b_1111_1000;
uint b = ~a;       // b = 0b_0000_0111;



シフト演算子

演算子解説
<<左辺の値を右辺の値だけビットを左へ移動
上位ビットは破棄され下位ビットには0が入る
uint a = 0b_1111_1010 << 2;
// a = 0b_1110_1000;
>>左辺の値を右辺の値だけビットを左へ移動
下位ビットは破棄され上位ビットには0が入る
uint a = 0b_1111_1010 >> 2;
// a = 0b_0011_1110;



演算子の優先順位

演算子解説
+x、-x、!x、~x、++x、--x単項
x * y、x / y、x % y乗法
x + y、x – y加法
x << y、x >> yシフト
x < y、x > y、x <= y、x >= y関係比較
x == y, x != y等価比較
x & y論理AND
x ^ y論理XOR
x | y論理OR
x && y条件付き論理AND
x || y条件付き論理OR
x ?? ynull合体
x = y代入



コメント