0

I am a bit confused as to what (~a).toString(2) is doing.

I'm familiar with binary and 1s complement and 2s complement.

So 5 is 000101 in 1s complement and 2s complement that number stays the same.

And -5 in 1s complement is 111010 and in 2s complement that's 111011

None of those numbers involve 110 or 100

So I don't understand how the binary results of 110 and 100 are being derived from the figure of 5 or of -5.

i also know that 1s complement is calculated by flipping bits, and I know that 2s complement is calculated by doing 1s complement and adding one. Or by a shortcut of holding all binary digits up to and including the first one you find going from the far right to the far left, and flipping the rest. And I know that for 1s complement and 2s complement of positive numbers you need one zero on the far left and more than that are ok but unnecessary. And that for negative numbers in 1s and 2s complement, you need at least one 1 on the far left, and more than that is ok but unnecessary. But none of those facts explain the result I see. (or perhaps they do but I can't see it)

I have read that tilda(~) flips bits but I don't see how this result is produced.

a=5
(~a).toString(2)
"-110"

and

a=-5
-5
(~a).toString(2)
"100"
Swapnil
  • 301
  • 1
  • 10
barlop
  • 12,887
  • 8
  • 80
  • 109
  • 2
    It's [Bitwise NOT](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_NOT)).... and the [radix 2](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString#Parameters) converts it to corresponding binary string – Pranav C Balan Oct 13 '16 at 10:24
  • 2
    `toString(2)` converts to binary, the tilde (`~`) is a bitwise unary operator http://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-work – evolutionxbox Oct 13 '16 at 10:25
  • `~5 == -6` and `~-5 == 4`. Mystery solved. – Niet the Dark Absol Oct 13 '16 at 10:26
  • @NiettheDarkAbsol that does clarify but leaves open the question of why. And one confusing thing about this is that in proper 1 and 2s complement, positive numbers start with 0. And negative numbers start with 1 and include no minus sign. – barlop Feb 19 '19 at 04:30

3 Answers3

2

~ is used to negate the bits in most programming languages. Now your variable is 5 and as javascript variables are 64 bit it is stored in memory as (0000000000000000000000000000000000000000000000000000000000000101)

after ~ the number in memory becomes (1111111111111111111111111111111111111111111111111111111111111010)

Now as it is a negative number (last bit being 1) it is calculated using 2's complement.

The two's complement value of which is -6.

Now the javascript is showing us -6 in binary where it is only showing 6 in binary and - to indicate a negative number i.e -110 === -6.

You should check out how 2's complement are written if you are confused.

Similarly to write -5 in binary first 5 is (000000.......000101)

flipping the bits(11111......1111010)

adding 1 it becomes (11111.....111011)

so -5 is (1111.....111011)

and doing ~(-5) flips the bits of above -5 so (0000....000100).

and as is a +ve number(1st bit is 0)

i.e it is shown as (100) === 4.

AKSHIT
  • 68
  • 8
  • You wrote "The two's complement value of which is -6." <-- The 2s complement of what is -6? Surely the 2s complement of (some number typically stated in decimal), is that number in 2s complement(which is of course binary)! The only value that I can see that would be 2s complement -6, is -6. – barlop Feb 19 '19 at 04:04
0
  1. As already described:"toString(2) - makes binary value from number"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString

var x = 6; 
console.log(x.toString(2));       // displays '110' 
  1. (~a) - is a Bitwise NOT (~).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

It Inverts all the bits. For example for 32 bit binary ~110 or

00 0000000000 0000000000 0000000110

11 1111111111 1111111111 1111111001

1 on the first position means negative number

фымышонок
  • 1,362
  • 16
  • 22
  • `(6).toString(2)=="110"` and `(~6).toString(2)=="-111"` I don't see 001 there – barlop Feb 19 '19 at 04:01
  • also I notice that the inversion of 6. (~6).toString(2) =="-111" and `(-1).toString(2)=="-1"` And technically, in 2s complement, you wouldn't have a minus sign.. 1111 is the same as 11 is the same as 1. it's all =-1. And why is (~6).toString(2) =="-111" If 6 is 110 then the inversion is 001 which is a positive number. So why should ~6.toString(2) be negative? – barlop Feb 19 '19 at 04:14
-2

Tilde has just an algorihm: -(N+1) So for example:

~0 is -(0+1) and this equals to -1

~6 is -(6+1) and this equals to -7

toString(2) - makes binary value from number

var a=1; a.toString(2); // 1
var a=2; a.toString(2); // 10
var a=3; a.toString(2); // 11
var a=4; a.toString(2); // 100
var a=5; a.toString(2); // 101
var a=6; a.toString(2); // 110
var a=7; a.toString(2); // 111
var a=8; a.toString(2); // 1000

Info about tilde: https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/

CroMagnon
  • 1,218
  • 7
  • 20
  • 32