想请教下关于异或运算

2019-02-22 17:30:19 +08:00
 lynnjyu

在 java 中 2518379987 ^ 0 得到的是 2518379987

但是在 js 中我尝试着用 2518379987 ^ 0 运算得到结果 -1776587309

为什么会有这两种不同的结果呢?

1128 次点击
所在节点    问与答
4 条回复
ballshapesdsd
2019-02-22 17:39:28 +08:00
补码吧,变成有符号 int 了?
yukiww233
2019-02-22 17:42:02 +08:00
GPIO
2019-02-22 17:45:26 +08:00
int x=2518379987;
unsigned int x1=2518379987;
int x2=x^0;
unsigned int x3=x1^0;
std::cout << x2 << " " << x3 << endl;

输出 -1776587309 2518379987
hahastudio
2019-02-22 17:50:28 +08:00
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/537721

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX