我希望看到二进制形式的正整数或负整数。

很像这个问题,但是是针对JavaScript的。


当前回答

我是这样处理的:

const decbin = nbr => {
  if(nbr < 0){
     nbr = 0xFFFFFFFF + nbr + 1
  }
  return parseInt(nbr, 10).toString(2)
};

从这个链接获得:https://locutus.io/php/math/decbin/

其他回答

这个答案试图用214748364810(231)- 900719925474099110(253-1)范围内的绝对值来处理输入。


在JavaScript中,数字以64位浮点表示形式存储,但位操作将它们强制转换为二进制补码格式的32位整数,因此任何使用位操作的方法都将输出范围限制为-214748364810(-231)- 214748364710(231-1)。

然而,如果避免按位操作,并且只使用数学运算来保留64位浮点表示法,则可以通过符号扩展53位的twosComplement,将任何安全整数可靠地转换为64位的二进制补码表示法:

function toBinary (value) { if (!Number.isSafeInteger(value)) { throw new TypeError('value must be a safe integer'); } const negative = value < 0; const twosComplement = negative ? Number.MAX_SAFE_INTEGER + value + 1 : value; const signExtend = negative ? '1' : '0'; return twosComplement.toString(2).padStart(53, '0').padStart(64, signExtend); } function format (value) { console.log(value.toString().padStart(64)); console.log(value.toString(2).padStart(64)); console.log(toBinary(value)); } format(8); format(-8); format(2**33-1); format(-(2**33-1)); format(2**53-1); format(-(2**53-1)); format(2**52); format(-(2**52)); format(2**52+1); format(-(2**52+1)); .as-console-wrapper{max-height:100%!important}

对于较旧的浏览器,存在以下函数和值的填充:

Number.isSafeInteger () Number.isInteger () 号码。MAX_SAFE_INTEGER String.prototype.padStart ()

作为额外的奖励,如果您使用BigInt对⌈64 / log2(基数)⌉数字中的负数执行两个补数转换,您可以支持任何基数(2-36):

function toRadix (value, radix) { if (!Number.isSafeInteger(value)) { throw new TypeError('value must be a safe integer'); } const digits = Math.ceil(64 / Math.log2(radix)); const twosComplement = value < 0 ? BigInt(radix) ** BigInt(digits) + BigInt(value) : value; return twosComplement.toString(radix).padStart(digits, '0'); } console.log(toRadix(0xcba9876543210, 2)); console.log(toRadix(-0xcba9876543210, 2)); console.log(toRadix(0xcba9876543210, 16)); console.log(toRadix(-0xcba9876543210, 16)); console.log(toRadix(0x1032547698bac, 2)); console.log(toRadix(-0x1032547698bac, 2)); console.log(toRadix(0x1032547698bac, 16)); console.log(toRadix(-0x1032547698bac, 16)); .as-console-wrapper{max-height:100%!important}

如果你对我的旧答案感兴趣,使用ArrayBuffer来创建一个Float64Array和Uint16Array之间的联盟,请参考这个答案的修订历史。

我们还可以计算正数或负数的二进制,如下所示:

函数toBinary (n) { Let binary = ""; 如果(n < 0) { N = N >>> 0; } while(Math.ceil(n/2) > 0){ 二进制= n%2 +二进制; n = Math.floor(n/2); } 返回二进制; } console.log (toBinary (7)); console.log (toBinary (7));

这就是解。事实上,这很简单

function binaries(num1){ 
        var str = num1.toString(2)
        return(console.log('The binary form of ' + num1 + ' is: ' + str))
     }
     binaries(3

)

        /*
         According to MDN, Number.prototype.toString() overrides 
         Object.prototype.toString() with the useful distinction that you can 
         pass in a single integer argument. This argument is an optional radix, 
         numbers 2 to 36 allowed.So in the example above, we’re passing in 2 to 
         get a string representation of the binary for the base 10 number 100, 
         i.e. 1100100.
        */

您可以编写自己的函数来返回一个比特数组。 示例如何将数字转换为比特

除数|被除数|位/余数

2 | | 1

2 4 | | 0

2 | 2 | 0

~ | | ~ 1

上面一行的例子:2 * 4 = 8,余数为1 9 = 1 0 0 1

function numToBit(num){
    var number = num
    var result = []
    while(number >= 1 ){
        result.unshift(Math.floor(number%2))
        number = number/2
    }
    return result
}

从下往上读余数。数字1从中间到上面。

对于32位来说,我想要的解决方案是这个答案末尾的代码,它来自developer.mozilla.org(MDN),但是添加了一些行,用于A)格式化和B)检查数字是否在范围内。

有些人建议使用x.toString(2),这对负号不起作用,它只是在负号中插入负号,这是不好的。

Fernando提到了一个简单的解决方案(x>>>0).toString(2);对于负数来说没问题,但是当x是正数时就有点问题了。它的输出从1开始,对于正数来说不是2s的补码。

任何不理解正数以0开始和负数以1开始的事实的人,在2s补中,可以在2s补中检查这个SO QnA。什么是“2的补”?

一个解决方案可能包括在正数前面加上一个0,这是我在这个答案的早期修订中所做的。我们有时可以接受一个33位的数字,或者我们可以确保要转换的数字在-(2^31)<=x<2^31-1的范围内。所以这个数字总是32位的。但是,您可以在mozilla.org上使用这个解决方案

Patrick的回答和代码很长,显然适用于64位,但有一个bug,评论者发现了,评论者修复了Patrick的bug,但Patrick在他的代码中有一些“神奇的数字”,他没有评论,已经忘记了,Patrick不再完全理解他自己的代码/为什么它可以工作。

安南有一些不正确和不清楚的术语,但提到了developer.mozilla.org的解决方案

注意-旧链接https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators现在重定向到其他地方,没有内容,但适当的旧链接,当archive.org检索页面时出现!,可在此下载https://web.archive.org/web/20150315015832/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

这里的解决方案适用于32位数字。

代码非常紧凑,只有三行函数。

但是我添加了一个正则表达式,以8位为一组格式化输出。基于如何用逗号作为千位分隔符格式化一个数字?(我只是修改了一下,从右到左按3秒分组,加逗号,到右到左按8秒分组,加空格)

并且,虽然mozilla对nMask的大小(输入的数字)做了一个评论..它必须在范围内,当数字超出范围时,他们没有测试或抛出错误,所以我添加了这个。

我不知道为什么他们将参数命名为“nMask”,但我将保持原样。

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

function createBinaryString(nMask) { // nMask must be between -2147483648 and 2147483647 if (nMask > 2**31-1) throw "number too large. number shouldn't be > 2**31-1"; //added if (nMask < -1*(2**31)) throw "number too far negative, number shouldn't be < -(2**31)" //added for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag < 32; nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1); sMask=sMask.replace(/\B(?=(.{8})+(?!.))/g, " ") // added return sMask; } console.log(createBinaryString(-1)) // "11111111 11111111 11111111 11111111" console.log(createBinaryString(1024)) // "00000000 00000000 00000100 00000000" console.log(createBinaryString(-2)) // "11111111 11111111 11111111 11111110" console.log(createBinaryString(-1024)) // "11111111 11111111 11111100 00000000" //added further console.log example console.log(createBinaryString(2**31 -1)) //"01111111 11111111 11111111 11111111"