Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB .NET calculating differently to Java

Can someone explain to me why .net is calculating these differently to Java

The equation

(-1646490243 << 4) + 3333 ^ -1646490243 + -957401312 ^ (-1646490243 >> 5) + 4

Java calculates it as

1173210151

.Net calculates as

-574040108

My issue is that I need .Net to calculate the same as Java as i'm porting across a decryption function and if it calculates differently then the decryption isn't going to be right.

Any help would be appreciated.

--Update--

Thanks guys, Xor was what I should of been using. Plus need to workaround Java not throwing an exception when an Integer number is too big.

Xor gives a result of -3121757145

(-1646490243 << 4) + 3333 Xor -1646490243 + -957401312 Xor (-1646490243 >> 5) + 4

Combine this with the answer from this link I found - Java sum 2 negative numbers. Gives the same result as Java

-3121757145 + 2 ^ 32 = 1173210151
like image 650
Seb Avatar asked Nov 12 '12 03:11

Seb


1 Answers

I checked the operator precedence table for Java and Visual Basic, they're the same regarding the operators in the expression. So it's not a problem with precedence.

Be aware though, in Visual Basic ^ is the operator for exponentiation, whereas Xor is the operator for exclusive or. That's different from Java, which uses the ^ operator as exclusive or and doesn't have an operator for exponentiation. All the other operators in the expression are the same in both languages

I can't tell from the code if the snippet is the one in Java or the one in Visual Basic - I'm guessing is in Java. If that's the case, it's quite possible that you've confused the exclusive or; try replacing ^ with Xor in the Visual Basic code and see if that fixes the problem:

(-1646490243 << 4) + 3333 Xor -1646490243 + -957401312 Xor (-1646490243 >> 5) + 4
like image 120
Óscar López Avatar answered Oct 22 '22 15:10

Óscar López