Node.js Bit Operations

September 12, 2013

I was working on trying to get the barometer readings from my TI SensorTag using Node.js when I came across this problem. See, the user's guide has two code examples for the algorithm for the pressure - one in C, and one in Java. The one in C uses primarily bit shift operations, whereas the one in Java uses Math.pow() to do the same thing. Naturally, I tended towards the bit shift operations since it makes the code a bit clearer as to what it's doing (ultimately, from a performance perspective, it doesn't matter since Math.pow(2, x) probably ultimately resolves to bit shifts anyway).

However, when I did this, I kept on getting pressure values that were all over the place. When I broke it down, it looked like the "offset" and "scale" factors, which rely heavily on bit shifts, were bouncing all over the place.

So, I did some looking, and discovered that, although Node.js stores all its variables as 64-bit floating point numbers, when it comes to bit operations, it does those as 32-bit numbers. A quick test showed that this is true:

> a = Math.pow(2, 31)-1
2147483647
> a >> 10
2097151
> a = Math.pow(2, 32) -1
4294967295
> a >> 10
-1
> a >> 2
-1

Notice the sharp transition once you hit that 32-bit border. What that meant was that any time I was doing bit operations, if the value I was operating on was less than 2^32, I got the right answer, otherwise I got garbage. Fun.

So, when I switched to the Java algorithm (modified, of course, because they seem to have forgotten to divide those values by 100 at the end) everything worked.

Good. To. Know.

Jack Lund

Jack Lund

comments powered by Disqus