/    Sign up×
Community /Pin to ProfileBookmark

UInt32 in JavaScript

I’ve got a hash algorithm in C# which I’d like to use in JavaScript but I’m having troubles porting it. I’ve located the part which doesn’t produce the correct result:

[CODE]UInt32 a = 682308529;
UInt32 b = 3202212377;
Console.WriteLine(a * b);[/CODE]

The result of this should be 438783561

I’ve tried with lots of bitwise operators but I can’t seem to get the same result. This is the approach I thought should work:

[CODE]var a = 682308529;
var b = 3202212377;
alert(a * b >>> 0);[/CODE]

However, the result of this is 438783488, close to what I expect but not exactly. Any ideas on this are much appreciated!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@RemoooauthorNov 05.2011 — Something else I don't really understand.

When I work with a higher precision in both languages in combination with the same operators I get different results:

JavaScript
[CODE]alert(682308529 * 3202212377 & 0xffffffff);[/CODE]

C#
[CODE]Console.WriteLine((Int64)682308529 * (Int64)3202212377 & 0xffffffff);[/CODE]

Shouldn't produce the same result?
Copy linkTweet thisAlerts:
@RemoooauthorNov 05.2011 — Okay, it took me quite some time to figure this out but the problem is not the convertion to UInt32 after the multiplication, it's actually the multiplication!

I've got two numbers up to 32bits but when I multiple them, I get a number which is too big for JavaScript numbers which means I already lost some information at this point.

I had to do some manual BigInt like multiplication as well as a custom shift functionality to convert to UInt32 afterwards. Quite a lot of work for something that simple!
Copy linkTweet thisAlerts:
@Jeff_MottNov 05.2011 — Sounds like you already figured out most of it for yourself. :-) Yes, numbers in JavaScript are double precision floating point, so they won't behave the same as integer multiplication.

Some time ago, I had to optimize for this exact scenario. Here's the result:

var n1 = 682308529;
var n2 = 3202212377;

<i> </i>var n1Low16 = n1 &amp; 0x0000ffff;
<i> </i>var n1High16 = n1 &gt;&gt;&gt; 16;

<i> </i>var n2Low16 = n2 &amp; 0x0000ffff;
<i> </i>var n2High16 = n2 &gt;&gt;&gt; 16;

<i> </i>var resultLow32 = (((n1 &amp; 0xffff0000) * n2) &gt;&gt;&gt; 0) + (((n1 &amp; 0x0000ffff) * n2) &gt;&gt;&gt; 0) &gt;&gt;&gt; 0;
<i> </i>var resultHigh32 = n1High16 * n2High16 + ((((n1Low16 * n2Low16) &gt;&gt;&gt; 17) + n1Low16 * n2High16) &gt;&gt;&gt; 15);

<i> </i>alert(resultLow32 == 438783561 ? 'pass' : 'fail');
Copy linkTweet thisAlerts:
@RemoooauthorNov 05.2011 — Jeff, this is great! Nicer than what I'm playing around with!

Just ran my unit tests, 100% success.

Thanks a lot, really appreciate your message!
×

Success!

Help @Remooo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...