6

I have the following code :

var a = parseInt('010001',2);
console.log(a.toString(2));
//  10001
var b = ~a;
console.log(b.toString(2));
// -10010

The MSDN Say

~ Performs the NOT operator on each bit. NOT a yields the inverted value (a.k.a. one's complement) of a.

010001 should thus return this 101110.

This Topic kinda confirm that

So I can't understand how we can get -10010 instead ? The only potential explanation is that:

010001 is negated 101110 but he write this -10001 and then for an obscure reason he give me the two complements and -10001 become -10010.

But all this is very obscure in my mind, would you have an idea on what happen precisely.

2
  • 3
    It looks like Javascript considers this to be a 32-bit signed int under the covers. 0b010001 = 0x00000011, or decimal 17. The one's complement of this is 0xFFFFFFEE, or decimal -18, when treated as a signed 32-bit int, which equates to 0x11111111,11111111,11111111,11101110. Sep 11, 2015 at 16:18
  • Also, 0b10010 is positive 18, so -0b10010 is -18. Sep 11, 2015 at 16:20

4 Answers 4

3

JavaScript's bitwise operators convert their operands to 32-bit signed integers (the usual 2's complement), perform their operation, then return the result as the most appropriate value in JavaScript's number type (double-precision floating point; JavaScript doesn't have an integer type). More in §12.5.11 (Bitwise NOT Operator ~) and §7.1.5 (ToInt32).

So your 10001 is:

00000000 00000000 00000000 00010001

which when ~ is:

11111111 11111111 11111111 11101110

...which is indeed negative in 2s complement representation.

You may be wondering: If the bit pattern is as above, then why did b.toString(2) give you -10010 instead? Because it's showing you signed binary, not the actual bit pattern. - meaning negative, and 10010 meaning 18 decimal. The bit pattern above is how that's represented in 2s complement bits. (And yes, I did have to go check myself on that!)

8
  • But that isn't -18 is it?
    – musefan
    Sep 11, 2015 at 16:26
  • 1
    @musefan: Turns out I don't have my bits wrong. 11111111 11111111 11111111 11101110 in 2s complement binary is -18 decimal. Sep 11, 2015 at 16:32
  • I thought negatives where just identify by the first bit, but Im too tired to think about this properly so see y'all Monday!
    – musefan
    Sep 11, 2015 at 16:34
  • @musefan: That would be in the bit pattern, yes, but not JavaScript's toString(2) output, which outputs a binary number with a number sign. Confusing, I know, I had to go double-check. Sep 11, 2015 at 16:35
  • Thanks a lot for you answer T.J Crowder i think i lack understanding on how 2s complement work ...
    – seb_kaine
    Sep 11, 2015 at 16:36
2

Under the covers, when Javascript does bitwise operations, it converts to a 32-bit signed integer representation, and uses that, then converts the result back into its internal decimal representation.

As such, your input value, 010001 becomes 00000000 00000000 00000000 00010001.

This is then inverted:

~00000000 00000000 00000000 00010001 => 11111111 11111111 11111111 11101110

Converted into hex, the inverted value is 0xFFFFFFEE, which is equivalent to the decimal value of -18.

Since this is a signed integer with a value of -18, this value is converted to the underlying decimal representation of -18 by Javascript.

When Javascript tries to print it as a base-2 number, it sees the negative sign and the value of 18, and prints it as -10010, since 10010 is the binary representation of positive 18.

2
1

JavaScript uses 32-bit signed numbers,so

a (010001) (17) is  0000 0000 0000 0000 0000 0000 0001 0001
b = ~a (?) (-18) is 1111 1111 1111 1111 1111 1111 1110 1110

The reason for printing -18 as -10010 and methods to get actual value is explained well here Link

1
  • Thanks for the extra precision AcidBurn !
    – seb_kaine
    Sep 11, 2015 at 16:41
0

As per the documentation on the Mozilla developer website here. Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6. That is why you are getting the negative sign in front of the number.

6
  • Not sure why the downvotes as this explain the problem perfectly
    – musefan
    Sep 11, 2015 at 16:22
  • This covers up the details of what's actually happening, but it's a nice rule of thumb to remember. If the OP is using bitwise operations, it's probably more correct to explain how they work than it is to provide an answer which glosses over them for sake of simplicity. Sep 11, 2015 at 16:23
  • This doesn't explain why, see T.J. Crowder's answer for a better explanation.
    – Etheryte
    Sep 11, 2015 at 16:23
  • It's not wrong, just it doesn't explain the problem. It just sais the result is to be expected, however without explaining why. Though it doesn't deserve the downvotes imo Sep 11, 2015 at 16:24
  • Thanks for your Help Rahul
    – seb_kaine
    Sep 11, 2015 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.