用语法糖强制算术优先级
Forcing arithmetic precedence with syntactic sugar
每天早上我起床喝杯咖啡,然后前往 SO 看看 John Skeet 前一天的回答。这是我每天的提醒,我不知道有多少。今天,为此 there was a discussion on the increment operators such as ++
and --
. The MSDN doc 表示 ++
和 --
的优先级高于 *
。考虑这段代码:
int c = 10;
c = c++ * --c;
和这个代码
int c = 10;
c = (c++) * (--c);
在这两种情况下 c
都是 100。您将如何强制优先级(如果可能的话),以便在乘法之前首先计算括号中的值?
变量后面的 ++ 只会在当前指令之后应用,因为它是一个 post 增量。
在变量前使用 ++ 或 -- 来实现你想要的
int c = 10;
c = ++c * --c ;
Console.WriteLine ( c ) ;
此代码将输出 110 因为 10 + 1 = 11 和 11 - 1 = 10 所以 10 * 11 = 110
计算机处理一堆操作。
以下...
int c = 10;
c = c++ * --c;
编译成这些操作...
//OpCode // Function Stack Var c
ldc.i4.s 10 // 10 is pushed onto the stack {10}
stloc.0 // pop the stack and store the value in location 0 {} 10
ldloc.0 // location 0 is pushed onto the stack {10} 10
dup // stack value is copied and pushed on stack {10,10} 10
ldc.i4.1 // 1 is added to the stack {10,10,1} 10
add // top 2 values are popped and the sum is pushed {10, 11} 10
stloc.0 // pop the stack and store the value in location 0 {10} 11
ldloc.0 // location 0 is pushed onto the stack {10} 11
ldc.i4.1 // 1 is added to the stack {10,11,1} 11
sub // top 2 values are popped and the difference is pushed {10, 10} 11
dup // stack value is copied and pushed on stack {10,10,10} 11
stloc.0 // pop the stack and store the value in location 0 {10,10} 10
mul // top 2 values are popped and the product is pushed {100} 10
stloc.0 // pop the stack and store the value in location 0 {} 100
ldloc.0 // location 0 is pushed onto the stack {100} 100
...我不确定是否清除了任何内容...但是您可以看到,当您在各处(stloc.0/ldloc.0)使用相同的变量时,实际操作在堆栈上进行。 (我在大括号 {} 中的内容)。操作(dup、add、sub 和 mul)不关心变量索引,只关心堆栈。
还有咯咯笑...
int c = 10;
c = ++c * c--;
...编译成这个...
//OpCode // Function Stack Var c
ldc.i4.s 10 // 10 is pushed onto the stack {10}
stloc.0 // pop the stack and store the value in location 0 {} 10
ldloc.0 // location 0 is pushed onto the stack {10} 10
ldc.i4.1 // 1 is added to the stack {10,1} 10
add // top 2 values are popped and the sum is pushed {11} 10
dup // stack value is copied and pushed on stack {11,11} 10
stloc.0 // pop the stack and store the value in location 0 {11} 11
ldloc.0 // location 0 is pushed onto the stack {11,11} 11
dup // stack value is copied and pushed on stack {11,11,11} 11
ldc.i4.1 // 1 is added to the stack {11,11,11,1} 11
sub // top 2 values are popped and the difference is pushed {11,11,10} 11
stloc.0 // pop the stack and store the value in location 0 {11,11} 10
mul // top 2 values are popped and the product is pushed {121} 10
stloc.0 // pop the stack and store the value in location 0 {} 121
ldloc.0 // location 0 is pushed onto the stack {121} 121
每天早上我起床喝杯咖啡,然后前往 SO 看看 John Skeet 前一天的回答。这是我每天的提醒,我不知道有多少。今天,为此 ++
and --
. The MSDN doc 表示 ++
和 --
的优先级高于 *
。考虑这段代码:
int c = 10;
c = c++ * --c;
和这个代码
int c = 10;
c = (c++) * (--c);
在这两种情况下 c
都是 100。您将如何强制优先级(如果可能的话),以便在乘法之前首先计算括号中的值?
变量后面的 ++ 只会在当前指令之后应用,因为它是一个 post 增量。 在变量前使用 ++ 或 -- 来实现你想要的
int c = 10;
c = ++c * --c ;
Console.WriteLine ( c ) ;
此代码将输出 110 因为 10 + 1 = 11 和 11 - 1 = 10 所以 10 * 11 = 110
计算机处理一堆操作。
以下...
int c = 10;
c = c++ * --c;
编译成这些操作...
//OpCode // Function Stack Var c
ldc.i4.s 10 // 10 is pushed onto the stack {10}
stloc.0 // pop the stack and store the value in location 0 {} 10
ldloc.0 // location 0 is pushed onto the stack {10} 10
dup // stack value is copied and pushed on stack {10,10} 10
ldc.i4.1 // 1 is added to the stack {10,10,1} 10
add // top 2 values are popped and the sum is pushed {10, 11} 10
stloc.0 // pop the stack and store the value in location 0 {10} 11
ldloc.0 // location 0 is pushed onto the stack {10} 11
ldc.i4.1 // 1 is added to the stack {10,11,1} 11
sub // top 2 values are popped and the difference is pushed {10, 10} 11
dup // stack value is copied and pushed on stack {10,10,10} 11
stloc.0 // pop the stack and store the value in location 0 {10,10} 10
mul // top 2 values are popped and the product is pushed {100} 10
stloc.0 // pop the stack and store the value in location 0 {} 100
ldloc.0 // location 0 is pushed onto the stack {100} 100
...我不确定是否清除了任何内容...但是您可以看到,当您在各处(stloc.0/ldloc.0)使用相同的变量时,实际操作在堆栈上进行。 (我在大括号 {} 中的内容)。操作(dup、add、sub 和 mul)不关心变量索引,只关心堆栈。
还有咯咯笑...
int c = 10;
c = ++c * c--;
...编译成这个...
//OpCode // Function Stack Var c
ldc.i4.s 10 // 10 is pushed onto the stack {10}
stloc.0 // pop the stack and store the value in location 0 {} 10
ldloc.0 // location 0 is pushed onto the stack {10} 10
ldc.i4.1 // 1 is added to the stack {10,1} 10
add // top 2 values are popped and the sum is pushed {11} 10
dup // stack value is copied and pushed on stack {11,11} 10
stloc.0 // pop the stack and store the value in location 0 {11} 11
ldloc.0 // location 0 is pushed onto the stack {11,11} 11
dup // stack value is copied and pushed on stack {11,11,11} 11
ldc.i4.1 // 1 is added to the stack {11,11,11,1} 11
sub // top 2 values are popped and the difference is pushed {11,11,10} 11
stloc.0 // pop the stack and store the value in location 0 {11,11} 10
mul // top 2 values are popped and the product is pushed {121} 10
stloc.0 // pop the stack and store the value in location 0 {} 121
ldloc.0 // location 0 is pushed onto the stack {121} 121