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

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


当前回答

一个简单的方法就是……

Number(42).toString(2);

// "101010"

其他回答

一个简单的方法就是……

Number(42).toString(2);

// "101010"

函数 dec2bin(dec) { return (dec >>> 0).toString(2); } console.log(dec2bin(1));1 console.log(dec2bin(-1));11111111111111111111111111111111 控制台.log(dec2bin(256));100000000 console.log(dec2bin(-256));11111111111111111111111100000000

您可以使用Number.toString(2)函数,但它在表示负数时存在一些问题。例如,(-1). tostring(2)输出为“-1”。

要解决这个问题,可以使用无符号右移位操作符(>>>)将数字强制转换为无符号整数。

如果你运行(-1 >>> 0). tostring(2),你将把你的数字向右移动0位,这不会改变数字本身,但它将表示为一个无符号整数。上面的代码将正确地输出“111111111111111111111111111111111111111111111111”。

这个问题有进一步的解释。

-3 >>> 0(右逻辑移位)将其参数强制为无符号整数,这就是为什么你得到了-3的32位2的补数表示。

这个答案试图用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之间的联盟,请参考这个答案的修订历史。

对于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"

你可以使用递归解决方案:

函数intToBinary(number, res = "") { If (number < 1) If (res === "")返回"0" 其他的 返回res 否则返回intToBinary(数学。楼层(数目/ 2),数目% 2 + res) } console.log (intToBinary (12)) console.log (intToBinary (546)) console.log (intToBinary (0)) console.log (intToBinary (125)) 只对正数有效。