发布于 2015-08-16 14:52:34 | 71 次阅读 | 评论: 0 | 来源: 网络整理
下列是D语言支持赋值操作符:
运算符 | 描述 | 示例 |
---|---|---|
= | 简单赋值运算符,分配从右侧操作数的值,以左侧操作数 | C = A + B will assign value of A + B into C |
+= | 添加和赋值操作符,它增加了右操作数为左操作数和结果分配给左操作数 | C += A is equivalent to C = C + A |
-= | 减和赋值操作符,它减去右边的操作数从左边的操作数,并将结果赋值给左操作数 | C -= A is equivalent to C = C - A |
*= | 乘法和赋值操作符,它乘以右边的操作数与左操作数和结果分配给左操作数 | C *= A is equivalent to C = C * A |
/= | 除法和赋值操作符,它分为左操作数与右边的操作数,并将结果赋值给左操作数 | C /= A is equivalent to C = C / A |
%= | 模量和赋值操作符,它采用模使用两个操作数和结果分配给左操作数 | C %= A is equivalent to C = C % A |
<<= | 左移位并赋值运算符 | C <<= 2 is same as C = C << 2 |
>>= | 向右移位并赋值运算符 | C >>= 2 is same as C = C >> 2 |
&= | 按位AND赋值运算符 | C &= 2 is same as C = C & 2 |
^= | 按位异或和赋值运算符 | C ^= 2 is same as C = C ^ 2 |
|= | OR运算和赋值运算符 | C |= 2 is same as C = C | 2 |
试试下面的例子就明白了提供D编程语言的所有赋值运算符:
import std.stdio;
int main(string[] args)
{
int a = 21;
int c ;
c = a;
writefln("Line 1 - = Operator Example, Value of c = %dn", c );
c += a;
writefln("Line 2 - += Operator Example, Value of c = %dn", c );
c -= a;
writefln("Line 3 - -= Operator Example, Value of c = %dn", c );
c *= a;
writefln("Line 4 - *= Operator Example, Value of c = %dn", c );
c /= a;
writefln("Line 5 - /= Operator Example, Value of c = %dn", c );
c = 200;
c = c % a;
writefln("Line 6 - %s= Operator Example, Value of c = %dn",'x25', c );
c <<= 2;
writefln("Line 7 - <<= Operator Example, Value of c = %dn", c );
c >>= 2;
writefln("Line 8 - >>= Operator Example, Value of c = %dn", c );
c &= 2;
writefln("Line 9 - &= Operator Example, Value of c = %dn", c );
c ^= 2;
writefln("Line 10 - ^= Operator Example, Value of c = %dn", c );
c |= 2;
writefln("Line 11 - |= Operator Example, Value of c = %dn", c );
return 0;
}
当编译并执行上面的程序它会产生以下结果:
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2