Solved

Xor in .NET, vs "^" in Javascript

Posted on 2004-09-16
8
911 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do Unit Testing with Session ? 1 41
Adjust the codes 3 63
Learning About The VB.NET TableLayoutPanel Control 5 42
SSIS Column mapping 5 40
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

732 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