8

I am converting some legacy Pascal to JavaScript. I need to multiple two 32-bit signed integers.

In the following sample loop some multiplications will cause overflow and will give negative numbers. This is intentional. I need to reproduce the same final number x at the end that matches the legacy system.

How can I do this in JavaScript to achieve the same result?

Here is some sample code:

var x = new Number(some value);  // I need this to be a 32-bit signed integer
var y = new Number(some value); // I need this to be a 32-bit signed integer

for (var i=0; i<100; i++) {   
    x = x * y; 
} 
return x;

2 Answers 2

10

Javascript's bitwise operators actually convert the value to a regular integer. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators This fact is used by things like asm.js to coerce the types, and you can do it yourself too. The trick is to put a |0 at the end of a number to force it to be 32 bit

function test() {
    var x = 255|0; // |0 does the type coercion
    var y = 255|0; // not strictly necessary at this var decl but used for explicitness

    for (var i=0; i<5; i++) {   
         x = (y * x)|0; // parens needed because |'s precedence
   } 

   return x;
}

I ran that with a few numbers and got the same result as C in Firefox.. didn't get a chance to test in IE, but I'm pretty sure this behavior is in the ECMAscript spec, so it should work.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think the parnes are required.
| has higher precedence than '*'. But my rule of thumb anyway is if you have to look up or even think more than a second about precedence rules, just write the parens to make is double-clear for the next reader anyway.
The | operator has a very low precedence, * has a very high precedence and is most likely to be executed first in operations. See this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
huh I should have looked it up... you are indeed right. But still, I prefer to write them anyway.
1

Math operations in JavaScript are always done as double-precision floating point. You'd have to write your own multiplication routine (or find one somewhere) to carry out integer math, and that'd be slow or hard (or both :).

7 Comments

This is one of the most critically weak aspects of the language, by the way.
What you say is true but the accepted answer above solves my current problem.
@Vic well it'll work for some values, but the multiplication operation itself is carried out with floating point hardware as a double-precision operation, so values near the limit of numeric capacity may get weird.
@Vic actually since there are plenty more mantissa bits than 32, it may work out OK.
So far my test data has shown the right results. Keeping my figures crossed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.