Byte handling in C #

I am experiencing a little problem when trying to convert some VB6 logic to C #. In one of the functions of VB6, it has the following statement:

w = Not CByte(w)

Where w is long.

In the example, after this line is evaluated in VB6, I see the following change:
Before: w = 110
After: w = 145

However, in C #, I rewrote a method containing 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:

w = ~(Convert.ToByte(w));

Finally, I was able to get the correct results with the following change:

w = ~(byte)w & 0xFF;

From what I can say, it looks like C # will convert it to sbyte, even if it is not specified for this. My question is: is there some kind of error in my logic? Is this the only way to get the equivalent of VB6?

+4
1

w - long, , a int, , .

w = ~(byte)w;

, w byte.., int, .

, ( : (byte)~w) :

w ^= 0xFF;

, , w byte.

+6

Source: https://habr.com/ru/post/1533192/


All Articles