JavaScript中二进制字符串的负数

2022-01-17 00:00:00 numbers tostring javascript

任何人都知道为什么 javascript Number.toString 函数不能正确表示负数?

Anyone knows why javascript Number.toString function does not represents negative numbers correctly?

//If you try
(-3).toString(2); //shows "-11"
// but if you fake a bit shift operation it works as expected
(-3 >>> 0).toString(2); // print "11111111111111111111111111111101"

我真的很好奇为什么它不能正常工作或者它以这种方式工作的原因是什么?我已经搜索了它,但没有找到任何有用的东西.

I am really curious why it doesn't work properly or what is the reason it works this way? I've searched it but didn't find anything that helps.

推荐答案

简答:

  1. toString() 函数接受小数,将其转换到二进制并添加一个-"号.

  1. The toString() function takes the decimal, converts it to binary and adds a "-" sign.

零填充右移将其操作数转换为有符号 32 位两个补码格式的整数.

A zero fill right shift converts it's operands to signed 32-bit integers in two complements format.

更详细的回答:

问题 1:

//If you try
(-3).toString(2); //show "-11"

在函数 .toString().当你通过 .toString() 输出一个数字时:

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

语法

numObj.toString([radix])

numObj.toString([radix])

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

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.

它采用十进制,将其转换为二进制并添加-"号.

It takes the decimal, converts it to binary and adds a "-" sign.

  1. 以 10 为底的3"转换为以 2 为底的为11"
  2. 添加一个符号给我们-11"

问题 2:

// but if you fake a bit shift operation it works as expected
        (-3 >>> 0).toString(2); // print "11111111111111111111111111111101"

零填充右移将其操作数转换为 带符号的 32 位整数.该操作的结果总是一个无符号的 32 位整数.

A zero fill right shift converts it's operands to signed 32-bit integers. The result of that operation is always an unsigned 32-bit integer.

所有位运算符的操作数都转换为有符号的 32 位二进制补码格式的整数.

The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format.

相关文章