我希望看到二进制形式的正整数或负整数。
很像这个问题,但是是针对JavaScript的。
我希望看到二进制形式的正整数或负整数。
很像这个问题,但是是针对JavaScript的。
当前回答
逻辑可以被任何编程语言实现的实际解决方案:
如果你确定它只是积极的:
var a = 0;
var n = 12; // your input
var m = 1;
while(n) {
a = a + n%2*m;
n = Math.floor(n/2);
m = m*10;
}
console.log(n, ':', a) // 12 : 1100
若能负或正——
(n >>> 0).toString(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之间的联盟,请参考这个答案的修订历史。
这是我的代码:
var x = prompt("enter number", "7");
var i = 0;
var binaryvar = " ";
function add(n) {
if (n == 0) {
binaryvar = "0" + binaryvar;
}
else {
binaryvar = "1" + binaryvar;
}
}
function binary() {
while (i < 1) {
if (x == 1) {
add(1);
document.write(binaryvar);
break;
}
else {
if (x % 2 == 0) {
x = x / 2;
add(0);
}
else {
x = (x - 1) / 2;
add(1);
}
}
}
}
binary();
函数 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的补数表示。
我用了一种不同的方法来解决这个问题。我决定在我的项目中不使用这段代码,但我想我会把它放在相关的地方,以防它对某人有用。
Doesn't use bit-shifting or two's complement coercion. You choose the number of bits that comes out (it checks for valid values of '8', '16', '32', but I suppose you could change that) You choose whether to treat it as a signed or unsigned integer. It will check for range issues given the combination of signed/unsigned and number of bits, though you'll want to improve the error handling. It also has the "reverse" version of the function which converts the bits back to the int. You'll need that since there's probably nothing else that will interpret this output :D
function intToBitString(input, size, unsigned) { if ([8, 16, 32].indexOf(size) == -1) { throw "invalid params"; } var min = unsigned ? 0 : - (2 ** size / 2); var limit = unsigned ? 2 ** size : 2 ** size / 2; if (!Number.isInteger(input) || input < min || input >= limit) { throw "out of range or not an int"; } if (!unsigned) { input += limit; } var binary = input.toString(2).replace(/^-/, ''); return binary.padStart(size, '0'); } function bitStringToInt(input, size, unsigned) { if ([8, 16, 32].indexOf(size) == -1) { throw "invalid params"; } input = parseInt(input, 2); if (!unsigned) { input -= 2 ** size / 2; } return input; } // EXAMPLES var res; console.log("(uint8)10"); res = intToBitString(10, 8, true); console.log("intToBitString(res, 8, true)"); console.log(res); console.log("reverse:", bitStringToInt(res, 8, true)); console.log("---"); console.log("(uint8)127"); res = intToBitString(127, 8, true); console.log("intToBitString(res, 8, true)"); console.log(res); console.log("reverse:", bitStringToInt(res, 8, true)); console.log("---"); console.log("(int8)127"); res = intToBitString(127, 8, false); console.log("intToBitString(res, 8, false)"); console.log(res); console.log("reverse:", bitStringToInt(res, 8, false)); console.log("---"); console.log("(int8)-128"); res = intToBitString(-128, 8, false); console.log("intToBitString(res, 8, true)"); console.log(res); console.log("reverse:", bitStringToInt(res, 8, true)); console.log("---"); console.log("(uint16)5000"); res = intToBitString(5000, 16, true); console.log("intToBitString(res, 16, true)"); console.log(res); console.log("reverse:", bitStringToInt(res, 16, true)); console.log("---"); console.log("(uint32)5000"); res = intToBitString(5000, 32, true); console.log("intToBitString(res, 32, true)"); console.log(res); console.log("reverse:", bitStringToInt(res, 32, true)); console.log("---");
逻辑可以被任何编程语言实现的实际解决方案:
如果你确定它只是积极的:
var a = 0;
var n = 12; // your input
var m = 1;
while(n) {
a = a + n%2*m;
n = Math.floor(n/2);
m = m*10;
}
console.log(n, ':', a) // 12 : 1100
若能负或正——
(n >>> 0).toString(2)