Search code examples
c#vb6xor

Exclusive OR (XOR) in vb6 versus c#


I am converting a vb6 program to c# and I cannot figure out why the results of my exclusive or operation are giving me different values.

The vb6 code:

'serialNum = 884167284, and dSize = 1953312760 here'
serialNum = serialNum Xor dSize       'serialNum = 1088322956 after this operation'
serialNum = serialNum Xor &HD7BF3A9C  'serialNum = -1755232496 after this operation'
GetDriveSerialNum = serialNum

The c# code:

//serialNum = 884167284, and dSize = 1953312760 here
serialNum = serialNum ^ dSize;       //serialNum = 1088322956 after this operation
serialNum = serialNum ^ 0xD7BF3A9C;  //serialNum = 2539734800 after this operation
return serialNum;

I need the c# code to give the negative number that is the result of the vb6 code. What can I do to achieve this?


Solution

  • A long in vb6 is a 32 bit signed value, so its range is - 2,147,483,468 to 2,147,483,468.

    A long in C# is a 64 bit signed value, so its range is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

    In the vb6 XOR you are overflowing, thus why the number is negative. To get the same value, you can use an int in C#.