Solved

Xor in .NET, vs "^" in Javascript

Posted on 2004-09-16
8
908 Views
Last Modified: 2012-06-21
When I do this in javascript:

3988292384 ^ 0

I get this:

-306674912

HOWEVER, in VB.net, when I evaluate 3988292384 Xor 0, I get 3988292384.

What's going on?
0
Comment
Question by:electricstory
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:vigrid
ID: 12076695
Maybe a hexadecimal mode will help:

3988292384 is 0x00000000EDB88320
-306674912 is 0xFFFFFFFFEDB88320

So JavaScript seems to convert the number to 64-bit form and do something weird about it, while VB does not.

HTH
0
 

Author Comment

by:electricstory
ID: 12076789
Very good observation! If you can figure out how I can emulate the behavior in .NET, you get all the points. (I don't deal much with bitwise and hex operations, so I'm a bit out of my depth.)
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12076957
Do you need to implement the JS behavior in .NET, or to implement .NET behavior in JS? Just to be sure before I code :)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:electricstory
ID: 12077038
The js behavior in .NET. I'm converting javascript code. Thanks!
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12077104
Sorry, I seem to have only the C# compiler installed on this machine. If you would go for the C# code, I could try and implement it in C#, but I can't do it in VB.NET right now.
0
 

Author Comment

by:electricstory
ID: 12077154
C# is fine.
0
 
LVL 4

Accepted Solution

by:
vigrid earned 500 total points
ID: 12077343
OK, I think I know exactly what happened (didn't realize that until started coding)

the value 3988292384 (dec) is 0xEDB88320 (hex). It's greater than 0x7FFFFFFF, so it should be considered negative value. The normal integer (31 bits value + 1 bit sign) type can't hold this value, however an unsigned integer (32 bits value) type can. So in order to preserve the sign of your input, JavaScript is treating this variable as a long (63 bits + 1 bit sign = 0xFFFFFFFFEDB88320). The way JavaScript performs the XOR operation seems to be weird. Order of value type conversions is not intuitive at the first glance. The XOR operation took 2 agruments of different types - the big number (64 bits) and zero (32 bits) and performed the XOR-ing only on the lower 32 bits, leaving the rest untact.

I don't know how do you want to use the XOR in your code, but I think that the System.Collections.BitArray can help you with the Xor method. I wouldn't emulate the misbehaving code in order to translate from one language to another...
0
 

Author Comment

by:electricstory
ID: 12077405
Okay, well that helps a good deal. Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question