c# - Calculation differences -


this question has answer here:

i have in c#

        byte a;         byte b;         byte c;          c = + b; 

and gives error c = + b , says "cannot implicitly convert type 'int' 'byte'. explicit conversion exists (are missing cast?). don't understand why because in bytes

matlab involved because translating image processing program matlab c# take values picture uint8 , doing calculations value when unit8 takes on , during calculations number higher 255 set 255. in c# made of variables bytes since under 255 anyways in example code when running calculations error pops up.

during calculations number higher 255 set 255.

this not supported natively in c#. instead, default behaviour of (byte) cast take least significant byte, giving arithmetic result equivalent modulo 256.

c = unchecked((byte)(200 + 200)); 

the result above 144, equivalent 400 % 256.

if want clip results @ 255, need specify explicitly:

c = (byte)math.min(a + b, 255); 

Comments