理解JavaScript按位NOT运算符和toString()函数

时间:2009-07-15 19:29:52

标签: javascript operator-overloading bit-manipulation

提前感谢所有人 -

alert((~1).toString(2));

输出:-10

但在PHP / Java中输出11111111111111111111111111111110

我错过了什么,为什么Javascript会在输出中添加“ - ”?

THX, 萨姆

5 个答案:

答案 0 :(得分:9)

我知道Java使用二进制补码表示负数,而 11111111111111111111111111111110 表示二进制,这是 ~1 给出的,表示-2。或者,以二进制形式表示带负号,-10,这就是你得到的。

使用二进制补码计算负10(在基数2中)的方法是先颠倒所有位,然后给出:

11111111111111111111111111111101

然后你加1,给你:

11111111111111111111111111111110

我猜Javascript也是如此。

答案 1 :(得分:9)

您可以使用移位运算符>>>在转换为二进制文件之前将数字转换为无符号整数:

(~1 >>> 0).toString(2) // "11111111111111111111111111111110"

答案 2 :(得分:3)

简答:

  1. 按位NOT (~1)执行1的补码转换 小数,它给我们-2
  2. .toString()函数基本上采用小数而不使用符号2,将其转换为二进制10并添加-符号,该符号为-10提供。
  3. 更详细的答案:

    它在函数.toString()中。当您通过.toString()

    输出数字时
      

    如果numObj为负数,则会保留该符号。就是这种情况   即使基数为2;返回的字符串是正二进制文件   numObj的表示前面带有 - 符号,而不是两个符号   numObj的补充。

    developer.mozilla.org我们得到这个计算1的整数补码的公式,当你在小数上执行NOT(〜)时使用它:

      

    按位注意任何数字x的产量 - (x + 1)。例如,约5个产量   -6。

    使用此表和示例可能会更好地解释:

    +-------------------------+-----+-----+-----+-----+-----+-----+------+
    | Base 10 Integer         | -3  | -2  | -1  | 0   | 1   | 2   | 3    |
    +-------------------------+-----+-----+-----+-----+-----+-----+------+
    | Base 10 1's Complement  |  2  |  1  |  0  | -1  | -2  | -3  | -4   |
    +-------------------------+-----+-----+-----+-----+-----+-----+------+
    | Base 2                  |     |     |     | 0   |  1  |  10 |  11  |
    +-------------------------+-----+-----+-----+-----+-----+-----+------+
    | Result ~x.toString(2)   | 10  |  1  |  0  | -1  | -10 | -11 | -100 |
    +-------------------------+-----+-----+-----+-----+-----+-----+------+
    
    1. 从Base 10整数“2”开始
    2. 基数为10的整数“2”,其1的补码为“-3”。这与执行NOT(〜)
    3. 相同
    4. .toString函数取无符号值(基数为10的“3”,基数为2的“11”)
    5. .toString函数添加“ - ”符号
    6. .toString输出“-11”

答案 3 :(得分:1)

这假设您正在使用32位......

var valueToNot = parseInt("11110000", 2);
var notResult = 0xFFFFFFFF - valueToNot;
console.log(notResult.toString(2));

结果 11111111111111111111111100001111

答案 4 :(得分:0)

这是一个在javascript中实现NOT的解决方案。它不漂亮,但它有效。


// Since ~ is the two's complement, then the one's complement is ~(num -1).
var num = 9;
num.toString(2);            //returns 1001
~(num - 1).toString(2);    //returns -1001
// WHAT the hell?? I guess the negative sign acts as a sign bit.

如果要在NOT(位切换)后查看小数的二进制字符串,请使用以下代码。

// Programer: Larry Battle
// Purpose: Provide a bit toggle function for javascript.
var getStrCopy = function (str, copies) {
    var newStr = str;
    copies = (copies > 0) ? copies : 1;
    while (--copies) {
        newStr += str;
    }
    return newStr;
};
var convertDecToBase = function ( dec, base, length, padding ) {
    padding = padding || '0' ;
    var num = dec.toString( base );
    length = length || num.length;
    if (num.length !== length) {
        if (num.length > length) {
            throw new Error("convertDecToBase(): num(" + num + ") > length(" + length + ") too long.");
        }
        num = getStrCopy( padding, (length - num.length)) + num;
    }
    return num;
};
var formatBinaryStr = function( str ){
    return str.replace( /\d{4}/g, '$& ' ).replace( /\s$/,'');
};
var toggleBits = function( dec, length, doFormat ){
    length = length || 8;
    var str = convertDecToBase( dec, 2, length || 8 );
    var binaryStr = str.replace( /0/g, 'o' ).replace( /1/g, '0').replace( /o/g, '1' );
    return ( doFormat ) ? formatBinaryStr( binaryStr ) : binaryStr ;
};

// The following requires Firebug or Google Chrome Dev Tools
clear();
console.log( toggleBits( 1 ) );    // returns "11111110"
console.log( toggleBits( 2 ) );    // returns "11111101"
console.log( toggleBits( 50, 16 ) );// returns "1111111111001101"
console.log( toggleBits( 15, 8, true ) );    // returns "1111 0000"
console.log( toggleBits( 520, 16, true ) ); //returns "1111 1101 1111 0111"