Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned Integer in Javascript

I'm working on a page that processes IP address information, but it's choking on the fact that integers are signed. I am using bitwise operators to speed it up, but the 64th bit (signed/unsigned flag) is messing it up.

Is there any way to force a number to be unsigned in Javascript? It seems to work fine, until subnet is greater than 30, or less than 2.

Try this:

<html>      <body>            <script type='text/javascript'>      document.write( (1 << 30) +"<br/>");      document.write( (1 << 31) +"<br/>");      document.write( (1 << 32) +"<br/>");      </script>            </body>  </html>

Result:

1073741824 -2147483648 1

like image 537
bradlis7 Avatar asked Dec 15 '09 16:12

bradlis7


People also ask

What is a unsigned integer?

An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation. The most significant byte is 0 and the least significant is 3.

What is a unsigned integer example?

The simplest numbers that we want to represent in the machine are the unsigned integers. These are whole numbers without a sign, for example, 0, 1, 2, 3, … The mechanical calculators of yesteryear and the car mileage meter of today both store unsigned integers on what are effectively cogs having ten numbered teeth1.

How do you declare unsigned integers?

The data type to declare an unsigned integer is: unsigned int and the format specifier that is used with scanf() and print() for unsigned int type of variable is "%u".

How do I use unsigned int?

Unsigned int is a data type that can store the data values from zero to positive numbers whereas signed int can store negative values also. It is usually more preferable than signed int as unsigned int is larger than signed int. Unsigned int uses “ %u ” as a format specifier.


2 Answers

document.write( (1 << 31) +"<br/>"); 

The << operator is defined as working on signed 32-bit integers (converted from the native Number storage of double-precision float). So 1<<31 must result in a negative number.

The only JavaScript operator that works using unsigned 32-bit integers is >>>. You can exploit this to convert a signed-integer-in-Number you've been working on with the other bitwise operators to an unsigned-integer-in-Number:

document.write(( (1<<31)>>>0 )+'<br />'); 

Meanwhile:

document.write( (1 << 32) +"<br/>"); 

won't work because all shift operations use only the lowest 5 bits of shift (in JavaScript and other C-like languages too). <<32 is equal to <<0, ie. no change.

like image 102
bobince Avatar answered Sep 28 '22 01:09

bobince


Douglas Crockford believes that bitwise operators is one of the bad parts of javascript:

In Java, the bitwise operators work with integers. JavaScript doesn't have integers. It only has double precision floating-point numbers. So, the bitwise operators convert their number operands into integers, do their business, and then convert them back. In most languages, these operators are very close to the hardware and very fast. In JavaScript, they are very far from the hardware and very slow. JavaScript is rarely used for doing bit manipulation.

-- Douglas Crockford in "JavaScript: The Good Parts", Appendix B, Bitwise Operators (emphasis added)

Are you sure that bitwise operators really speed up your logic?

like image 26
Artem Latyshev Avatar answered Sep 28 '22 02:09

Artem Latyshev