I'm experiencing a bit of an issue while trying to convert some VB6 logic into C#. In one of the VB6 functions, it has the following statement:
w = Not CByte(w)
Where w is a long
.
In an example, after this line evaluates in VB6, I can see the following change:
Before: w = 110
After: w = 145
However, in C#, I've rewritten the method to contain the following code:
w = ~(byte)w;
But, when I run the same example, I get these results, instead:
Before: w = 110
After: w = -111
I also get the same result doing:
w = ~(Convert.ToByte(w));
I was finally able to get the correct results with the following change:
w = ~(byte)w & 0xFF;
From what I can tell, it looks like C# is converting it to an sbyte
even though it's not specified to do so. My question is: is there some flaw in my logic? Is this the only way to get the VB6 equivalent?