Solved

Xor in .NET, vs "^" in Javascript

Posted on 2004-09-16
8
912 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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