Solved

Xor in .NET, vs "^" in Javascript

Posted on 2004-09-16
8
902 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
 

Author Comment

by:electricstory
ID: 12077038
The js behavior in .NET. I'm converting javascript code. Thanks!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now