表达式a ^ = b ^ = a ^ = b;工作? [英] how does the expression a^=b^=a^=b; work?

查看:143
本文介绍了表达式a ^ = b ^ = a ^ = b;工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()
{

int a=10,b=20;

a^=b^=a^=b;

printf("%d%d",a,b);

}
a) a=20,b=10
b) a=10,b=20
c) Syntax error
d) Unpredictable

Ans : a=20 b=10 





可以解释一下。



can somebody explain it.

推荐答案

要解释什么?



^ = 运算符获取的按位异或第一和第二个操作数;将结果存储在第一个操作数指定的对象中。



确切地说:

What to explain?

^= operator obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand.

It means exactly:
a=a^b;
b=b^a;
a=a^b;





http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators [ ^ ]


它是XOR运算符所以基本上会发生这样的事情:



a = a ^ b



0000 1010

0001 0100

---------------------------

0001 1110 = a



b = b ^ a



0001 0100

0001 1110

- -------------------------

0000 1010 = b(12月10日)



a = a ^ b



0001 1110

0000 1010

- -------------------------

0001 0100 = a(12月20日)



是的,我很无聊^^
it's the XOR operator so basically what happens is this:

a = a^b

0000 1010
0001 0100
---------------------------
0001 1110 = a

b = b^a

0001 0100
0001 1110
---------------------------
0000 1010 = b (Dec: 10)

a = a^b

0001 1110
0000 1010
---------------------------
0001 0100 = a (Dec: 20)

And yes, I'm bored^^


这篇关于表达式a ^ = b ^ = a ^ = b;工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆