如何在JS中将十进制转换为二进制?

Sha*_*lav 5 javascript binary decimal converter

可以通过以下方式将二进制转换为十进制:

var binary = "110";
var int = parseInt(binary, 2);
document.getElementById("results").innerHTML = int;
Run Code Online (Sandbox Code Playgroud)
<div id="results"></div>
Run Code Online (Sandbox Code Playgroud)

但是,我如何做相反的操作:将 int 转换为二进制?

小智 13

let decimal = prompt('please insert decimal number');
console.log(Number(decimal).toString(2));
Run Code Online (Sandbox Code Playgroud)


Eud*_*rpa 6

Dec 到 Bin:原始(按位)

/**
*   Dec to Bin 
*   with bitwise operations
*
*   Eudes Serpa M.
**/

const numberToConvert = 5;
const numberOfBits = 32; // 32-bits binary
const arrBitwise = [0]; // save the resulting bitwise

for (let i=0; i<numberOfBits; i++) {
    let mask = 1;

    const bit = numberToConvert & (mask << i); // And bitwise with left shift

    if(bit === 0) {
        arrBitwise[i] = 0;
    } else {
        arrBitwise[i] = 1;
    }
}

const binary = arrBitwise.reverse().join("");

console.log(`This is the resulting binary: ${binary}`)
console.log(`This is the verification ${parseInt(binary, 2)}`);
Run Code Online (Sandbox Code Playgroud)

解释:

  • 第 2 行:我们指定组成生成的二进制文件的位数。

  • 第 3 行:我们定义一个数组,用于保存位级操作产生的位。最后,这将是我们生成的二进制文件(反转它)

  • 用于:用于“创建”位的二进制。

  • Mask:表示我们在位级别移位到的数字(1 进行 AND 运算并获得要转换的数字的 1 中的位)。

  • bit:执行操作的结果位,例如:

    位数 = 3;

    掩码=1;

    for (i = 0 -> 31) { // 32 位

      // Explanation of the operation to obtain the bit in position i
    
      // ---- For i = 0;
      1. mask << 0 = ...0001 (a 1 in decimal), since it does not do any shifting.
    
      2. 3 & 1
          /* At the bit level we have to
               3 = ...0011
               1 = ...0001,
               so when doing the AND operation at the bit level, we have to:
           0011
          &0001
          ------
          0001 === 1 decimal
       */
    
      // bit then takes the value resulting from the previous operations. This is: 
      bit = 1;
    
      // The if is not meet, so it enters the else:
      arrBitwise[0] = 1;
    
      // ---- For i = 1;
      1. mask << 1 = ...0010 (a 2 in decimal)
    
      2. 3 & 2
          /* At the bit level we have to
               3 = ...0011
               2 = ...0010,
               so when doing the AND operation at the bit level, we have to:
          0011
         &0010
         -------
          0010 === 2 decimal
       */
    
      // bit then takes the value resulting from the previous operations. This is: bit = 2;
    
      // The if is not meet, so it enters the else:
      arrBitwise[1] = 1;
    
    
      // ----- For i = 2;
      1. mask << 2 = ...0100 (a 4 in decimal)
    
      2. 3. 4
          /* At the bit level we have to
               3 = ...0011
               4 = ...0100,
               so when doing the AND operation at the bit level, we have to:
          0011
         &0100
        -------
          0000 === 0 decimal
       */
    
      // bit then takes the value resulting from the previous operations. This is: 
      bit = 0;
    
     // The if meet, so:
      arrBitwise[2] = 0;
    
    Run Code Online (Sandbox Code Playgroud)

    }

因此,arrBitwise 将是: arrBitwise = [1, 1, 0, 0, ..., 0];

arrBitwise.reverse() // [0, ..., 0, 0, 1, 1]
Run Code Online (Sandbox Code Playgroud)

与 .join()

"0...0011"
Run Code Online (Sandbox Code Playgroud)

二进制表示3。

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

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


小智 2

var x = 6;
console.log(x.toString(2));
Run Code Online (Sandbox Code Playgroud)

  • 请解释。 (4认同)
  • 就像我在这个帖子的另一个答案中提出的其他评论一样。您需要说明为什么以及如何您的答案最适合 OP。 (4认同)