| 右 | ||
| 逻辑与 | & | 左 |
| 逻辑异或 | ^ | 左 |
| 逻辑或 | 左 | |
| 条件与 | && | 左 |
| 条件或 | 左 | |
| 条件 | ?: | 右 |
| 赋值等 | = *= /= %= += -= <<= >>= &= ^= = | 右 |
| 2.清单 1-2. 单目运算符: unary.cs |
| using system; class unary { public static void main() { int unary = 0; int preincrement; int predecrement; int postincrement; int postdecrement; int positive; int negative; sbyte bitnot; bool lognot; preincrement = ++unary; console.writeline("pre-increment: {0}", preincrement); predecrement = --unary; console.writeline("pre-decrement: {0}", predecrement); postdecrement = unary--; console.writeline("post-decrement: {0}", postdecrement); postincrement = unary++; console.writeline("post-increment: {0}", postincrement); console.writeline("final value of unary: {0}", unary); positive = -postincrement; console.writeline("positive: {0}", positive); negative = +postincrement; console.writeline("negative: {0}", negative); bitnot = 0; bitnot = (sbyte)(~bitnot); console.writeline("bitwise not: {0}", bitnot); lognot = false; lognot = !lognot; console.writeline("logical not: {0}", lognot); } } |
| 说明 |
1.当计算表达式的时候,在后置增一和后置减一运算符进行运算时,先返回其值,再进行增一或者减一运算。当使用前置加号和减号运算符进行运算时,是先进行增一或者减一的运算,然后再返回其结果值。
2.在清单1-2中, 变量unary初始化为0,进行++x 运算时,"unary"的值加1,再把其值1赋给"preincrement"变量。在进行--x运算时,先把"unary"的值减到0, 再把值0赋给"predecrement"变量。
3.进行x-运算时,先把"unary"的值0赋给"postdecrement" 变量,之后再把"unary"减到-1。进行x++运算时,先把"unary"的值-1赋给"postincrement"变量,之后再对"unary"加1,使得"unary"变量现在的值为0。
4.变量"bitnot"初始值为0,进行按位取反运算,本例中,数0表示为二进制"00000000",按位取反之后变为-1,其二进制表示为"11111111"。
5.了解一下表达式"(sbyte)(~bitnot)", 任何对类型sbyte, byte, short 或者 ushort 类型数据的运算,返回结果都是整数。要把值赋给bitnot变量,我们必须使用cast (类型)运算符(强制类型转换),其中type表示你希望转换成的类型(本例中为sbyte)。 cast运算符把大范围类型的数据转换为小范围类型的数据时,须特别谨慎,因为此时有丢失数据的危险。一般来说,把小类型的数据赋给大类型变量,并没有问题, 因为大范围数据类型的变量具有足够的空间存放小类型数据。 注意在signed 和unsigned类型之间进行cast运算时,也存在此类危险。 许多初级程序设计教程对变量的位表示作出了很好的讲解,同时也介绍了直接进行cast运算的危险。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器