最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

"Simulate" a 32-bit integer overflow in JavaScript - Stack Overflow

programmeradmin1浏览0评论

JavaScript can handle the following Math just fine:

var result = (20000000 * 48271) % 0x7FFFFFFF;

But in some programming languages, that first int*int multiplication results in a value too large to hold in a standard 32 bit integer. Is there any way to "simulate" this in JavaScript, and see what the resulting calculation would be if the multiplication resulted in an integer overflow?

JavaScript can handle the following Math just fine:

var result = (20000000 * 48271) % 0x7FFFFFFF;

But in some programming languages, that first int*int multiplication results in a value too large to hold in a standard 32 bit integer. Is there any way to "simulate" this in JavaScript, and see what the resulting calculation would be if the multiplication resulted in an integer overflow?

Share Improve this question edited May 10, 2014 at 6:31 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked May 10, 2014 at 6:20 IQAndreasIQAndreas 8,4688 gold badges43 silver badges75 bronze badges 4
  • 1 possible duplicate of Does Javascript handle integer overflow and underflow? If yes, how? – Félix Saparelli Commented May 10, 2014 at 6:30
  • 1 @FélixSaparelli It's not a duplicate, 20000000 * 48271 is still well within the JavaScript Number's 52 bit accuracy; it will not overflow. I'm trying to simulate a 32 bit overflow. – IQAndreas Commented May 10, 2014 at 6:33
  • I cheated by subtracting 2^32 enough times from the result, but I don't think that's very efficient or very smart :P – BoltClock Commented May 10, 2014 at 6:34
  • if Math.abs(int * int) is greater than (2^32)/2 then log value and continue? – Xotic750 Commented May 10, 2014 at 6:45
Add a comment  | 

3 Answers 3

Reset to default 8

In newer browsers, Math.imul(a,b) will give you an actual 32-bit integer multiplied result, with overflow resulting the way you would expect (it gives the lower half of the 64-bit result as what it returns).

However, as far as I know there's no way to actually get the overflow, (the upper 32 bits) but the modulus you showed in your answer gets rid of that information, so I figure that's not what you want. If they were going to do overflow, they'd have to separate it based on signed and unsigned anyway.

I know this works in Chrome, Firefox, and Opera, not sure about the rest, though pretty sure IE doesn't have it (typical). You'd need to fall back to a shim such as this one.

It is possible to simulate 32-bit integer by "abusing" the bitwise operators available in JavaScript (since they can only return integers within that range).

To convert to a signed 32-bit integer:

x = (a * b) | 0;

To convert to an unsigned 32-bit integer:

x = (a * b) >>> 0;

Another way to achieve this is to convert to a format where you can remove extra bytes before converting back to integer. That may not be optimal for JS but can be helpful in environments with really restricted operators.

For instance with hexadecimal:

var hugeInteger = 999999999999999;
var ui32 = parseInt(hugeInteger.toString(16).slice(-8), 16);
// ui32 == 2764472319

It's also possible to get the overflow from hugeInteger.toString(16).slice(0,-8)

发布评论

评论列表(0)

  1. 暂无评论