Solved

Xor in .NET, vs "^" in Javascript

Posted on 2004-09-16
8
906 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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

26 Experts available now in Live!

Get 1:1 Help Now