思归呓语

衣带渐宽终不悔,为伊消得人憔悴
随笔 - 413, 评论 - 3007, 引用 - 245

导航

关于

标签

每月存档

最新留言

  • re:xUnit.net
    <p>collection of woman's dresses and casual dresses on this site <a href="http://www.o...
    by dress(注册) on 2010/9/1 17:35:51
  • reply
    Free time is a great stuff but not any student is able to use that. Naturally college students stay ...
    by Kerr30SYBIL(注册) on 2010/8/27 23:51:37
  • re:【翻译】Entity Framework 特性CTP全程示范: Code Only
    <p> <p>Welcome to fashion goods online store,</p> <p>[b][url=http://www.chan...
    by chicken24(注册) on 2010/8/27 17:59:53
  • re:《C#与.NET 3.0高级程序设计(特别版)》
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:11:15
  • re:【翻译】实体框架中的POCO支持 - 第三部分 - POCO的变动跟踪
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:08:14
  • re:【翻译】Entity Framework 特性CTP全程示范: Code Only
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:08:06
  • re:【翻译】Code Only增强
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:07:59
  • re:站在巨人的肩膀上
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:07:48
  • re:Oslo
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:01:44
  • re:Oslo
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:01:28
  • reply this post
    The superb data referring to this topic must be free for people, because they demand <a href=&quo...
    by ClariceFlowers27(注册) on 2010/6/20 4:43:26
  • answer this topic
    Are you willing to get <a href="http://www.prime-resume.com">buy resume</a>, ...
    by BeverleyCopeland20(注册) on 2010/6/18 19:35:15
  • respond this topic
    I really loved the post. Although specially I think authority is satisfactory . As long as it does...
    by HodgesLara(注册) on 2010/6/18 3:46:16
  • respond this post
    People have an opportunity order the buy research paper and <a href="http://www.gogetessays....
    by FINCHMorgan(注册) on 2010/6/17 19:39:43
  • answer this topic
    Different persons should discus often about your superior article. I guess that, the best <a href...
    by AlisaEmerson25(注册) on 2010/6/15 6:11:01

广告

 

对于

6。 整数交换

Java: 
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.out.println("x = " + x + "; y = " + y);

C#:
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.Console.WriteLine("x = " + x + "; y = " + y);

输出结果一样

Java x = 0; y = 1984
C# x = 0; y = 1984

fancyf 在上个post的回复中建议这个是Bug,其实不然,因为根据Specifications,

x ^= y ^= x ^= y;

其运算是这么做的(起码当前版本是如此):

"...The order in which operands in an expression are evaluated, is left to right. [Example: For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence. end example]..." (见C# Language Specification, 14.2;至于Java中的表达式运算次序,参考 Java Language Specification, 15.7)

^=处,即对于 a ^= expr ,做运算时,a = a ^ expr,右边的a用的是运算expr的值,这样

y ^= x ^= y;  ==> x=1984^2001 =>17, y = 2001 ^ 17 => 1984
x ^= y ^= x ^= y; ==> x = 1984 ^ 1984 => 0

让我们再来看一下对应下面C#编码的IL码,观察stack的变化

public class CleverSwap {
    public static void Main(string[] args) {
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.Console.WriteLine("x={0}, y={1}",x,y);
    }
}

.method public hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       49 (0x31)
  .maxstack  4
  .locals init (int32 V_0,
           int32 V_1)
  IL_0000:  nop
  IL_0001:  ldc.i4     0x7c0  //stack: 0x7c0
  IL_0006:  stloc.0  //stack:  <==>x=0x7c0
  IL_0007:  ldc.i4     0x7d1 //stack: 0x7d1 
  IL_000c:  stloc.1  //stack:  <==>x=0x7c0,y=0x7d1
  IL_000d:  ldloc.0  //stack: 0x7c0 
  IL_000e:  ldloc.1  //stack: 0x7c0,0x7d1
  IL_000f:  ldloc.0  //stack: 0x7c0,0x7d1,0x7c0
  IL_0010:  ldloc.1  //stack: 0x7c0,0x7d1,0x7c0,0x7d1
  IL_0011:  xor   //stack: 0x7c0,0x7d1,0x11
  IL_0012:  dup   //stack: 0x7c0,0x7d1,0x11,0x11 
  IL_0013:  stloc.0  //stack: 0x7c0,0x7d1,0x11     <==>x=0x11,y=0x7d1
  IL_0014:  xor   //stack: 0x7c0,0x7c0     <==>x=0x11,y=0x7d1
  IL_0015:  dup   //stack: 0x7c0,0x7c0,0x7c0     <==>x=0x11,y=0x7d1
  IL_0016:  stloc.1  //stack: 0x7c0,0x7c0     <==>x=0x11,y=0x7c0
  IL_0017:  xor   //stack: 0x00     <==>x=0x11,y=0x7c0
  IL_0018:  stloc.0  //stack:  <==>x=0x00,y=0x7c0
  IL_0019:  ldstr      "x={0}, y={1}"
  IL_001e:  ldloc.0
  IL_001f:  box        [mscorlib]System.Int32
  IL_0024:  ldloc.1
  IL_0025:  box        [mscorlib]System.Int32
  IL_002a:  call       void [mscorlib]System.Console::WriteLine(string,
                                                                object,
                                                                object)
  IL_002f:  nop
  IL_0030:  ret
} // end of method CleverSwap::Main

至于C/C++,根据Joshua Bloch/Neal Gafter

"...... In C and C++, the order of expression evaluation is not specified. When compiling the expression x ^= expr, many C and C++ compilers sample the value of x after evaluating expr, which makes the idiom work. Although it may work, it runs afoul of the C/C++ rule that you must not modify a variable repeatedly between successive sequence points [ISO-C]. Therefore, the behavior of this idiom is undefined even in C and C++....."

而Java/C#则明确规定了表达式中operand的evaluation次序,所以结果不同,起码当前版本是如此

打印 | 张贴于 2005-08-21 23:56:00 | Tag:.NET  书籍

留言反馈

#上周技术关注:.NET和J2EE该相互学习什么 编辑
Ping Back来自:blog.csdn.net
2005-08-29 02:11:00 | [匿名:曾登高]
#《也做个比较》的解释(草稿) 编辑
Ping Back来自:blog.csdn.net
2005-08-24 02:51:00 | [匿名:fancyf]
#re: 也做个比较(2) 编辑
如梦方醒。
x ^= y ^= x ^= y;所生成的汇编代码非常精练(没想到JIT的优化这么好),看起来更容易懂:
0000005e mov esi,edi
00000060 xor edi,ebx
00000062 xor ebx,edi
00000064 xor esi,ebx
00000066 mov edi,esi
前面的定义部分我省略了,edi对应x,ebx对应y,esi是一个临时变量,也就是说
x ^= y ^= x ^= y;
等价于
int temp = x;
x ^= y;
y ^= x;
temp ^= y;
x = temp;
2005-08-23 00:11:00 | [匿名:fancyf]
#re: 也做个比较(2) 编辑
晕,看到这么多代码这晕,呵呵
2005-08-22 18:13:00 | [匿名:allen]
#re: 也做个比较(2) 编辑
x,y=y,x

:)
2005-08-22 14:39:00 | [匿名:rIPPER]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0