V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lynnjyu
V2EX  ›  问与答

想请教下关于异或运算

  •  
  •   lynnjyu · 2019-02-22 17:30:19 +08:00 · 1074 次点击
    这是一个创建于 1883 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 java 中 2518379987 ^ 0 得到的是 2518379987

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

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

    4 条回复    2019-02-22 17:50:28 +08:00
    ballshapesdsd
        1
    ballshapesdsd  
       2019-02-22 17:39:28 +08:00
    补码吧,变成有符号 int 了?
    yukiww233
        2
    yukiww233  
       2019-02-22 17:42:02 +08:00
    GPIO
        3
    GPIO  
       2019-02-22 17:45:26 +08:00   ❤️ 1
    int x=2518379987;
    unsigned int x1=2518379987;
    int x2=x^0;
    unsigned int x3=x1^0;
    std::cout << x2 << " " << x3 << endl;

    输出 -1776587309 2518379987
    hahastudio
        4
    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.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1456 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:19 · PVG 01:19 · LAX 10:19 · JFK 13:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.