14

Thanks to everyone in advance:

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

This outputs: -10

But in PHP/Java it outputs 11111111111111111111111111111110

Am I missing something? Why does Javascript add a "-" to the output?

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
  • Interesting question. I suspect it's something to do with JavaScript being a dynamic language and treating the 1 as a different type in some way, though this will need further elucidation. – Noldorin Jul 15 '09 at 19:36
  • No, it's just that ~1 is -2. JavaScript treats results of bitwise operations as two's complement numbers. – Nosredna Jul 15 '09 at 19:47
  • What happens if you cast to a signed int in PHP or Java? – Nosredna Jul 15 '09 at 19:49

5 Answers5

9

You can use the shift operator >>> to convert the number to an unsigned integer before converting to binary:

(~1 >>> 0).toString(2) // "11111111111111111111111111111110"
Brendan Annable
  • 2,637
  • 24
  • 37
9

I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got.

The way you calculate the negative of 10 (in base 2) using two's complement is that you first invert all of the bits, giving you:

11111111111111111111111111111101

then you add 1, giving you:

11111111111111111111111111111110

I guess the same is happening in Javascript.

IRBMe
  • 4,335
  • 1
  • 21
  • 22
  • 1
    In JavaScript, ~1 is -2. -2 in binary is -10. – Nosredna Jul 15 '09 at 19:46
  • Yes. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Bitwise_Operators – Chetan S Jul 15 '09 at 19:47
  • So basically the output that I am getting in java/php is 2's complement of -2? If i was working with that number doing something like: alert(parseInt("11111111111111111111111111111110", 2)); I get "4294967294", so this sort of implies that you have to know that the number is in 2's complement before hand? Thanks again! –  Jul 15 '09 at 20:46
  • You can't tell if a binary number represents a signed or an unsigned number. In fact, you can't tell if it represents an integer, a floating point number or anything else. The way you know how to interpret a binary number is by the type of the variable that it's stored in. – IRBMe Jul 16 '09 at 17:00
3

Short answer:

  1. A bitwise NOT (~1) performs a 1's complement conversion of the decimal which gives us -2
  2. The .toString() function basically takes the decimal without the sign 2, converts it to binary 10 and adds a - sign which gives us -10.

A more detailed answer:

It's in the function .toString(). When you output a number via .toString():

If the numObj is negative, the sign is preserved. This is the case even if the radix is 2; the string returned is the positive binary representation of the numObj preceded by a - sign, not the two's complement of the numObj.

Taken from the developer.mozilla.org we got this formula that calculates the 1's complement of an integer, this is used when you perform a NOT (~) on a decimal:

Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6.

Maybe it's better explained with this table and an example:

+-------------------------+-----+-----+-----+-----+-----+-----+------+
| 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. Starting with Base 10 integer "2"
  2. Base 10 integer "2" its 1's Complement is "-3". This is the same as performing a NOT (~)
  3. .toString function take the unsigned value (= "3" in base 10 and = "11" in base 2)
  4. .toString function adds a "-" symbol
  5. .toString outputs "-11"
Daan
  • 7,685
  • 5
  • 43
  • 52
2

This assumes that you are working in 32 bits...

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

results in 11111111111111111111111100001111

John
  • 3,716
  • 2
  • 19
  • 21
0

Here's a solution to implement NOT in javascript. It ain't pretty but it works.


// 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.

If you want to view the Binary String of a decimal after a NOT (bit Toggle), then use the following code.

// 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"

Larry Battle
  • 9,008
  • 4
  • 41
  • 55