/    Sign up×
Community /Pin to ProfileBookmark

Javascript XOR Odditites

I have run into a snag with the JavaScript ^ operator for a calculation that I need to do in parallel in Delphi and in JS. Here is an example. Take two numbers

0xD9 11011001

0x9E3779B9 10011110001101110111100110111001

Delphi gives 9E377960

10011110001101110111100101100000 – the result I expect.

Javascript gives

-1640531616 = 1111111111111111111111111111111110011110001101110111100101100000

Could anyone shed some light on this mystery and suggest a way to get both Delphi and JS to behave in the same way? I would be most obliged for any help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherFeb 04.2007 — JavaScript's bitwise operators use signed 32 bit arithmetic. If you need an unsigned 32 bit XOR you could use the >>> operator:function U32BitXOR(a, b) {
return ((a >>> 1) ^ (b >>> 1)) * 2 + (a % 2 ^ b % 2)
}
Copy linkTweet thisAlerts:
@FredAtauthorFeb 05.2007 — JavaScript's bitwise operators use signed 32 bit arithmetic. If you need an unsigned 32 bit XOR you could use the >>> operator:function U32BitXOR(a, b) {
return ((a >>> 1) ^ (b >>> 1)) * 2 + (a % 2 ^ b % 2)
}
[/QUOTE]


Thanks. In the mean time I had figured out this

JS_XOR(a,b){return (a|b) & ~(a&b)}
×

Success!

Help @FredAt 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...