views:

704

answers:

5

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.

Maybe I'm expecting too much out of a simple language.

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

+5  A: 

Javascript doesn't have integers, all numbers are actually doubles.

The Javascript 1.5 Reference by Mozilla suggests that one can only use bitwise-operations safely for 32 bit numbers.

Georg
I read somewhere that when you do bitwise operations, it's converted to an integer, and back to the given type (double, I would assume).
bradlis7
You know, I've looked at that page 100 times, and didn't see "32-bit". Maybe I'll go back to using Math.pow(2,n), instead of (1 << n). It'll take a few more processing loops in theory, but at least it will work.
bradlis7
Math.pow won't help you if you use the shift or bitwise boolean operators on the result as it'll be coerced to a 32-bit signed integer; e.g. alert(Math.pow(2,31)|1) yields -2147483647
moonshadow
@moonshadow, that's because, you put an | in the equation. I changed them all to using Math.pow, and it works fine.
bradlis7
A: 

What kind of IP addresses do you have? IPv4 uses only 32bit addresses, so JavaScript should be fine (using double which gives you an 52bit integer part). IPv6 uses 128bit addresses, so you'll have to use an array. My guess is that something else is broken.

[EDIT] Build a small library which uses an array of two ints as the internal data type.

Aaron Digulla
I thought about a library, but it's too much effort unless it's the only option. You're right about 32/64-bit, but the bitwise operators work only on 32-bit integers for some reason.
bradlis7
+4  A: 

Use >>> instead of >> to get an unsigned right shift instead of a sign-extending one. All the other bitwise operators behave the same way regardless of whether ints are signed or not.

Your code breaking "when subnet ... is less than 2" is concerning. That sounds like you may have some bug unrelated to signedness of integers.

moonshadow
The problem is not the subnet, is that I compute both the Number of Networks, and Number of Hosts. One is going to be 2^31 (which is 1 << 31) if n = 1.
bradlis7
And what's wrong with that? 1<<31 gives you the correct value - namely, it has bit 31 set. Bitwise logic - masks, shifts etc - will behave as you expect.Now, if you actually want to *display* a value greater than ((1<<31)-1) in the form of an integer in the range 2147483647..4294967296, you're going to have to do some nasty thing or other, e.g. alert(x<0?4294967296+x:x) which works by coercing x back to the double representation internally.
moonshadow
+5  A: 
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 -1.

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.

bobince
+1: Very informative. I was wondering why the `1 << 32` resulted in a 1 and not 0.
Joel Potter
Good information. It's too bad Javascript is so limited, it's so much easier for me to design interfaces in HTML. They should add PyScript as a language in Firefox :).
bradlis7
+2  A: 

Douglas Crockford believes that bitwise operators is one of the bad parts of javascript (book - JavaScript: The Good Parts):

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.

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

BaykaJIak
That's true. Honestly, I have not done much programming outside of high-level languages. I just assumed that doing `1 << 24` would be faster than `Math.pow(2,24)` in processing speed. I have changed all of my code to use `pow`, and it seems to be just as fast.
bradlis7