Javascript左移,是否对有符号或无符号整数进行操作?

时间:2017-06-13 22:47:34

标签: javascript bit-manipulation elm

我有一个单元测试套件,在我的Windows开发机器上永远不会失败,但在Travis CI中它总是在Debian上失败。两者都在运行节点v6.9.1。

它抱怨4294967295不等于-1。值429496729是通过分别向左移动0xFF 0,8,16和24步生成的,并将它们“或”运算在一起,从而有效地生成值0xFFFFFFFF-1是文字0xFFFFFFFF

标准中有没有说明这应该如何运作?我的代码应该是2^32-1还是-1?至少有一些东西说实现应该坚持签名或未签名的行为,但不是两者都有吗?

Elm elm-test测试:

test "bytesToInts packs maxint ok" <|
  \_ -> bytesToInts [ 0xFF, 0xFF, 0xFF, 0xFF ] |> Expect.equal [ 0xFFFFFFFF ]

原始榆树功能:

bytesToInts : List Int -> List Int
bytesToInts ints =
  case ints of
    a :: b :: c :: d :: rest ->
      List.foldl
        Bitwise.or
        0
        [ a |> Bitwise.shiftLeftBy 24
        , b |> Bitwise.shiftLeftBy 16
        , c |> Bitwise.shiftLeftBy 8
        , d |> Bitwise.shiftLeftBy 0
        ]
        :: bytesToInts rest

    [] ->
      []

    _ ->
      Debug.crash "pack4"

编译到这个:

var _elm_lang$core$Native_Bitwise = function() {

return {
    and: F2(function and(a, b) { return a & b; }),
    or: F2(function or(a, b) { return a | b; }),
    xor: F2(function xor(a, b) { return a ^ b; }),
    complement: function complement(a) { return ~a; },
    shiftLeftBy: F2(function(offset, a) { return a << offset; }),
    shiftRightBy: F2(function(offset, a) { return a >> offset; }),
    shiftRightZfBy: F2(function(offset, a) { return a >>> offset; })
};

}();

和这个

var _user$project$Base64$bytesToInts = function (ints) {
    var _p6 = ints;
    if (_p6.ctor === '::') {
        if (((_p6._1.ctor === '::') && (_p6._1._1.ctor === '::')) && (_p6._1._1._1.ctor === '::')) {
            return {
                ctor: '::',
                _0: A3(
                    _elm_lang$core$List$foldl,
                    _elm_lang$core$Bitwise$or,
                    0,
                    {
                        ctor: '::',
                        _0: _p6._0 << 24,
                        _1: {
                            ctor: '::',
                            _0: _p6._1._0 << 16,
                            _1: {
                                ctor: '::',
                                _0: _p6._1._1._0 << 8,
                                _1: {
                                    ctor: '::',
                                    _0: _p6._1._1._1._0 << 0,
                                    _1: {ctor: '[]'}
                                }
                            }
                        }
                    }),
                _1: _user$project$Base64$bytesToInts(_p6._1._1._1._1)
            };
        } else {
            return _elm_lang$core$Native_Utils.crashCase(
                'Base64',
                {
                    start: {line: 83, column: 3},
                    end: {line: 99, column: 26}
                },
                _p6)('pack4');
        }
    } else {
        return {ctor: '[]'};
    }
};

0 个答案:

没有答案