w
is a long
, but let's say it's an int
, it doesn't really matter except it's slightly easier to explain.
w = ~(byte)w;
Ok, so w
is cast to a byte
.. and then immediately back to int
because that's what arithmetic operations do.
You could solve it by taking Justin's suggestion from the comments (which does the cast at the right moment: (byte)~w
), or with this:
w ^= 0xFF;
That is not strictly the same thing though, it's different if w
starts out with a value outside of the range of a byte
.