連載

改訂版 C#入門

第8章 式と演算子

(株)ピーデー
川俣 晶
2002/10/09

Page1 Page2 Page3


 本記事は、(株)技術評論社が発行する書籍『新プログラミング環境 C#がわかる+使える』から許可を得て転載したものです。同書籍に関する詳しい情報については、本記事の最後に掲載しています。

 データを加工する最も基本的な手段は、「+」や「-」などの演算子を使用することである。本章では、式を記述するために必要なC#の演算子について解説する。

8-1 C#の演算子

 足し算などの基本的な演算子はプログラミング言語が変わっても表記が変わらないことが多い。例えば、足し算を示すために「+」記号を使うことは、FORTRANの昔からC#の現在まで踏襲されてきた基本的な表現方法である。むしろ、コンピュータができる前の数学の表記からずっと踏襲されてきたといってもよいかもしれない。また、C#はC/C++の順当な発展形として進化してきたという事情から、多くの演算子はC/C++と同様に振る舞う。そこで、本章のテーマは演算子なのだが、必ずしもすべての読者がすべての演算子の解説を必要とはしていないと考えた。まずは本章のサンプル・プログラムの実行結果を見てほしい。この結果を1行ずつ見て、自分が理解しているとおりの結果が出ているなら、その演算子についての説明を読む必要はないだろう。なぜその結果になるのか分からない演算子があったときに、解説を読んでいただきたい。

 では、先に、サンプル・ソースの実行結果をFig.8-1に示す。

   1: ● 基本演算(みんな理解しよう)
   2: 2+3 =   5
   3: 2147483647+1  =   -2147483648
   4: 2147483647u+1u  =   2147483648
   5: 4294967295u+1u  =   0
   6: 3-2 =   1
   7: -2147483648-1   =   2147483647
   8: 0u-1u   =   4294967295
   9: 3*2 =   6
  10: 2147483647*2  =   -2
  11: 2147483647u*2u  =   4294967294
  12: 6/3 =   2
  13: 6/5 =   1
  14: 6.0/5.0 =   1.2
  15: 6%3 =   0
  16: 6%5 =   1
  17: 1<2 =   True
  18: 2<1 =   False
  19: 3<3 =   False
  20: 1>2 =   False
  21: 2>1 =   True
  22: 3>3 =   False
  23: 1<=2  =   True
  24: 2<=1  =   False
  25: 3<=3  =   True
  26: 1>=2  =   False
  27: 2>=1  =   True
  28: 3>=3  =   True
  29: 1==2  =   False
  30: 2==1  =   False
  31: 3==3  =   True
  32: 1!=2  =   True
  33: 2!=1  =   True
  34: 3!=3  =   False
  35: 1 is int  =   True
  36: 1.0 is int  =   False
  37: false&false =   False
  38: true&false  =   False
  39: true&true   =   True
  40: false^false =   False
  41: true^false  =   True
  42: true^true   =   False
  43: false|false =   False
  44: true|false  =   True
  45: true|true   =   True
  46: false ? 1 : 2   =   2
  47: true ? 1 : 2  =   1
  48: +(2+3)  =   5
  49: -(2+3)  =   -5
  50: !true   =   False
  51: !false  =   True
  52: ● ビット演算(ビット表現が分かる人のみ)
  53: 256>>1  =   128
  54: 256<<1  =   512
  55: 2147483647>>1   =   1073741823
  56: 2147483647<<1   =   -2
  57: 2147483647u>>1  =   1073741823
  58: 2147483647u<<1  =   4294967294
  59: -2147483648>>1  =   -1073741824
  60: -2147483648<<1  =   0
  61: 1&3 =   1
  62: 1^3 =   2
  63: 1|3 =   3
  64: ~1  =   -2
  65: ~1u =   4294967294
  66: ~-1 =   0
  67: ● 変数の値を変える演算子(ソースと見比べて理解しよう)
  68: set x=0
  69: ++x =   1
  70: ++x =   2
  71: ++x =   3
  72: set x=0
  73: x++ =   0
  74: x++ =   1
  75: x++ =   2
  76: set x=0
  77: --x =   -1
  78: --x =   -2
  79: --x =   -3
  80: set x=0
  81: x-- =   0
  82: x-- =   -1
  83: x-- =   -2
  84: set x=0
  85: x=1 =   1
  86: x is now 1
  87: x=1; x+=2; value of x is 3
  88: x=1; x-=2; value of x is -1
  89: x=2; x*=3; value of x is 6
  90: x=6; x/=2; value of x is 3
  91: x=6; x%=2; value of x is 0
  92: x=6; x<<=2; value of x is 24
  93: x=6; x>>=2; value of x is 1
  94: x=1; x&=3; value of x is 1
  95: x=1; x^=3; value of x is 2
  96: x=1; x|=3; value of x is 3
  97: ● 式を処理しない場合がある演算子(ソースと見比べて理解しよう)
  98: set x=0
  99: returnFalse(x++) && returnFalse(x++)  =   False
 100: x is now 1
 101: set x=0
 102: returnTrue(x++) && returnTrue(x++)  =   True
 103: x is now 2
 104: set x=0
 105: returnFalse(x++) || returnFalse(x++)  =   False
 106: x is now 2
 107: set x=0
 108: returnTrue(x++) || returnTrue(x++)  =   True
 109: x is now 1
Fig.8-1

 Fig.8-1の結果は、List 8-1のソース・コードより出力したものである。

   1: using System;
   2:
   3: namespace Sample001
   4: {
   5:   class Class1
   6:   {
   7:     public static bool returnTrue( int value )
   8:     {
   9:       return true;
  10:     }
  11:     public static bool returnFalse( int value )
  12:     {
  13:       return false;
  14:     }
  15:     [STAThread]
  16:     static void Main(string[] args)
  17:     {
  18:       Console.WriteLine("● 基本演算(みんな理解しよう)");
  19:       Console.WriteLine("2+3\t=\t{0}",2+3);
  20:       int x1=2147483647, y1=1;
  21:       Console.WriteLine("2147483647+1\t=\t{0}",x1+y1);
  22:       Console.WriteLine("2147483647u+1u\t=\t{0}",2147483647u+1u);
  23:       uint x2=4294967295u, y2=1u;
  24:       Console.WriteLine("4294967295u+1u\t=\t{0}",x2+y2);
  25:
  26:       Console.WriteLine("3-2\t=\t{0}",3-2);
  27:       int x3=-2147483648, y3=1;
  28:       Console.WriteLine("-2147483648-1\t=\t{0}",x3-y3);
  29:       uint x4=0u, y4=1u;
  30:       Console.WriteLine("0u-1u\t=\t{0}",x4-y4);
  31:
  32:       Console.WriteLine("3*2\t=\t{0}",3*2);
  33:       int x5=2147483647, y5=2;
  34:       Console.WriteLine("2147483647*2\t=\t{0}",x5*y5);
  35:       Console.WriteLine("2147483647u*2u\t=\t{0}",2147483647u*2u);
  36:
  37:       Console.WriteLine("6/3\t=\t{0}",6/3);
  38:       Console.WriteLine("6/5\t=\t{0}",6/5);
  39:       Console.WriteLine("6.0/5.0\t=\t{0}",6.0/5.0);
  40:
  41:       Console.WriteLine("6%3\t=\t{0}",6%3);
  42:       Console.WriteLine("6%5\t=\t{0}",6%5);
  43:
  44:       Console.WriteLine("1<2\t=\t{0}",1<2);
  45:       Console.WriteLine("2<1\t=\t{0}",2<1);
  46:       Console.WriteLine("3<3\t=\t{0}",3<3);
  47:       Console.WriteLine("1>2\t=\t{0}",1>2);
  48:       Console.WriteLine("2>1\t=\t{0}",2>1);
  49:       Console.WriteLine("3>3\t=\t{0}",3>3);
  50:
  51:       Console.WriteLine("1<=2\t=\t{0}",1<=2);
  52:       Console.WriteLine("2<=1\t=\t{0}",2<=1);
  53:       Console.WriteLine("3<=3\t=\t{0}",3<=3);
  54:       Console.WriteLine("1>=2\t=\t{0}",1>=2);
  55:       Console.WriteLine("2>=1\t=\t{0}",2>=1);
  56:       Console.WriteLine("3>=3\t=\t{0}",3>=3);
  57:
  58:       Console.WriteLine("1==2\t=\t{0}",1==2);
  59:       Console.WriteLine("2==1\t=\t{0}",2==1);
  60:       Console.WriteLine("3==3\t=\t{0}",3==3);
  61:
  62:       Console.WriteLine("1!=2\t=\t{0}",1!=2);
  63:       Console.WriteLine("2!=1\t=\t{0}",2!=1);
  64:       Console.WriteLine("3!=3\t=\t{0}",3!=3);
  65:
  66:       Console.WriteLine("1 is int\t=\t{0}",1 is int);
  67:       Console.WriteLine("1.0 is int\t=\t{0}",1.0 is int);
  68:
  69:       Console.WriteLine("false&false\t=\t{0}",false&false);
  70:       Console.WriteLine("true&false\t=\t{0}",true&false);
  71:       Console.WriteLine("true&true\t=\t{0}",true&true);
  72:
  73:       Console.WriteLine("false^false\t=\t{0}",false^false);
  74:       Console.WriteLine("true^false\t=\t{0}",true^false);
  75:       Console.WriteLine("true^true\t=\t{0}",true^true);
  76:
  77:       Console.WriteLine("false|false\t=\t{0}",false|false);
  78:       Console.WriteLine("true|false\t=\t{0}",true|false);
  79:       Console.WriteLine("true|true\t=\t{0}",true|true);
  80:
  81:       Console.WriteLine("false ? 1 : 2\t=\t{0}",false ? 1 : 2);
  82:       Console.WriteLine("true ? 1 : 2\t=\t{0}",true ? 1 : 2);
  83:
  84:       Console.WriteLine("+(2+3)\t=\t{0}",+(2+3));
  85:       Console.WriteLine("-(2+3)\t=\t{0}",-(2+3));
  86:
  87:       Console.WriteLine("!true\t=\t{0}",!true);
  88:       Console.WriteLine("!false\t=\t{0}",!false);
  89:
  90:       Console.WriteLine("● ビット演算(ビット表現が分かる人のみ)");
  91:       Console.WriteLine("256>>1\t=\t{0}",256>>1);
  92:       Console.WriteLine("256<<1\t=\t{0}",256<<1);
  93:
  94:       Console.WriteLine("2147483647>>1\t=\t{0}",2147483647>>1);
  95:       Console.WriteLine("2147483647<<1\t=\t{0}",2147483647<<1);
  96:
  97:       Console.WriteLine("2147483647u>>1\t=\t{0}",2147483647u>>1);
  98:       Console.WriteLine("2147483647u<<1\t=\t{0}",2147483647u<<1);
  99:
 100:       Console.WriteLine("-2147483648>>1\t=\t{0}",-2147483648>>1);
 101:       Console.WriteLine("-2147483648<<1\t=\t{0}",-2147483648<<1);
 102:
 103:       Console.WriteLine("1&3\t=\t{0}",1&3);
 104:       Console.WriteLine("1^3\t=\t{0}",1^3);
 105:       Console.WriteLine("1|3\t=\t{0}",1|3);
 106:
 107:       Console.WriteLine("~1\t=\t{0}",~1);
 108:       Console.WriteLine("~1u\t=\t{0}",~1u);
 109:       Console.WriteLine("~-1\t=\t{0}",~-1);
 110:
 111:       Console.WriteLine("● 変数の値を変える演算子(ソースと見比べて理解しよう)");
 112:       int x=0; Console.WriteLine("set x=0");
 113:       Console.WriteLine("++x\t=\t{0}",++x);
 114:       Console.WriteLine("++x\t=\t{0}",++x);
 115:       Console.WriteLine("++x\t=\t{0}",++x);
 116:
 117:       x=0; Console.WriteLine("set x=0");
 118:       Console.WriteLine("x++\t=\t{0}",x++);
 119:       Console.WriteLine("x++\t=\t{0}",x++);
 120:       Console.WriteLine("x++\t=\t{0}",x++);
 121:
 122:       x=0; Console.WriteLine("set x=0");
 123:       Console.WriteLine("--x\t=\t{0}",--x);
 124:       Console.WriteLine("--x\t=\t{0}",--x);
 125:       Console.WriteLine("--x\t=\t{0}",--x);
 126:
 127:       x=0; Console.WriteLine("set x=0");
 128:       Console.WriteLine("x--\t=\t{0}",x--);
 129:       Console.WriteLine("x--\t=\t{0}",x--);
 130:       Console.WriteLine("x--\t=\t{0}",x--);
 131:
 132:       x=0; Console.WriteLine("set x=0");
 133:       Console.WriteLine("x=1\t=\t{0}", x=1 );
 134:       Console.WriteLine("x is now {0}",x);
 135:
 136:       x = 1;
 137:       x += 2;
 138:       Console.WriteLine("x=1; x+=2; value of x is {0}", x );
 139:       x = 1;
 140:       x -= 2;
 141:       Console.WriteLine("x=1; x-=2; value of x is {0}", x );
 142:       x = 2;
 143:       x *= 3;
 144:       Console.WriteLine("x=2; x*=3; value of x is {0}", x );
 145:       x = 6;
 146:       x /= 2;
 147:       Console.WriteLine("x=6; x/=2; value of x is {0}", x );
 148:       x = 6;
 149:       x %= 2;
 150:       Console.WriteLine("x=6; x%=2; value of x is {0}", x );
 151:       x = 6;
 152:       x <<= 2;
 153:       Console.WriteLine("x=6; x<<=2; value of x is {0}", x );
 154:       x = 6;
 155:       x >>= 2;
 156:       Console.WriteLine("x=6; x>>=2; value of x is {0}", x );
 157:       x = 1;
 158:       x &= 3;
 159:       Console.WriteLine("x=1; x&=3; value of x is {0}", x );
 160:       x = 1;
 161:       x ^= 3;
 162:       Console.WriteLine("x=1; x^=3; value of x is {0}", x );
 163:       x = 1;
 164:       x |= 3;
 165:       Console.WriteLine("x=1; x|=3; value of x is {0}", x );
 166:
 167:       Console.WriteLine("● 式を処理しない場合がある演算子(ソースと見比べて理解しよう)");
 168:       x=0; Console.WriteLine("set x=0");
 169:       Console.WriteLine("returnFalse(x++) && returnFalse(x++)\t=\t{0}",returnFalse(x++) && returnFalse(x++));
 170:       Console.WriteLine("x is now {0}",x);
 171:       x=0; Console.WriteLine("set x=0");
 172:       Console.WriteLine("returnTrue(x++) && returnTrue(x++)\t=\t{0}",returnTrue(x++) && returnTrue(x++));
 173:       Console.WriteLine("x is now {0}",x);
 174:       x=0; Console.WriteLine("set x=0");
 175:       Console.WriteLine("returnFalse(x++) || returnFalse(x++)\t=\t{0}",returnFalse(x++) || returnFalse(x++));
 176:       Console.WriteLine("x is now {0}",x);
 177:       x=0; Console.WriteLine("set x=0");
 178:       Console.WriteLine("returnTrue(x++) || returnTrue(x++)\t=\t{0}",returnTrue(x++) || returnTrue(x++));
 179:       Console.WriteLine("x is now {0}",x);
 180:     }
 181:   }
 182: }
List 8-1
 

 INDEX
  改訂版 C#入門 
  第8章 式と演算子
  8-1 C#の演算子
    8-2 四則演算子(+、-、*、/)
    8-11 代入演算子(=、+=、-=など)
 
連載:改訂版 C#入門

TechTargetジャパン

Insider.NET フォーラム 新着記事

@ITメールマガジン 新着情報やスタッフのコラムがメールで届きます(無料)

RSSフィード

キャリアアップ

- PR -
@IT Sepcial

イベントカレンダー

PickUpイベント

- PR -
もっと見る
- PR -

お勧め求人情報

ホワイトペーパーTechTargetジャパン

@IT Sepcial
ソリューションFLASH