Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Decimal to Binary - 64 bit

The binary for the decimal -805306368 is:

11111111111111111111111111111111 11010000000000000000000000000000

However, in Javascript I get the following:

var str = parseInt(-805306368).toString(2);
document.write(str);
-110000000000000000000000000000

Can anyone explain how to parse the 64 bit binary string from this decimal?

like image 537
StuR Avatar asked Jun 07 '12 17:06

StuR


People also ask

How to convert decimal number into binary number in JavaScript?

Users can follow the below syntax to use the toString() method to convert decimal to binary. let decimal = 10; // to convert positive decimal to binary let binary = decimal. toString( redix ); // to convert negative decimal to binary Let binary = (decimal >>> 0). toString( redix );

How do you write binary in JavaScript?

Use the string "b" to write a binary file. Use the string "ba" to append to a binary file. Use the string "a" to append to a text file. Use any other string or omit the parameter to write to a text file.

How do I convert decimal to binary?

How is the Decimal to Binary Conversion Done? The simplest way to convert a decimal number to a binary number is by dividing the given number repeatedly by 2 until we get 0 as the quotient. Then, we write the remainders in the reverse order to get the binary value of the given decimal number.

What is binary in JavaScript?

JavaScript Uses 32 bits Bitwise Operands JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers. Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers.


2 Answers

JavaScript does not use the two's-complement representation, it uses the hyphen - character in front of the string to represent negative numbers. That's because two's-complement representation requires to know the length of bits. When turning a two's-complement number with a certain number of bits into one with more bits (e.g., when copying from a one-byte variable to a two-byte variable), the most-significant bit must be repeated in all the extra bits.

To get the expected result, you could invert each bit but it doesn't provide the result we want:

>>> (~-805306368).toString(2)
"101111111111111111111111111111"

Yet, JavaScript does all binary operations on 32-bit integers, so this won't work for bigger (or smaller) numbers and at least will be very confusing. So, you would need to implement your own formatting algorithm.

// example of 32-bit-conversion:
>>> (~parseInt("1111111111111111111111111111111",2)).toString(2)
"-10000000000000000000000000000000"
>>> (~parseInt("11111111111111111111111111111111",2)).toString(2)
"0"

My Implementation:

function get64binary(int) {
  if (int >= 0)
    return int
      .toString(2)
      .padStart(64, "0");
  else
    return (-int - 1)
      .toString(2)
      .replace(/[01]/g, d => +!+d) // hehe: inverts each char
      .padStart(64, "1");
}

console.log(get64binary(805306368))
console.log(get64binary(-805306368))
like image 174
Bergi Avatar answered Nov 03 '22 00:11

Bergi


You can use parseInt() again. It has an optional second parameter, that enables you to specify the radix (or base) of the number in the string you are trying to parse.

Such as: parseInt("-110000000000000000000000000000", 2) // gives -805306368

like image 26
Sune Rasmussen Avatar answered Nov 02 '22 23:11

Sune Rasmussen