Why is a modulo operator (%) result implicitly cast to the left side rather than the right side in C#? -
take following code:
long longinteger = 42; int normalinteger = 23; object rem = longinteger % normalinteger;
if rem
remainder of longinteger / normalinteger
, shouldn't remainder bounded smaller sized "int", divisor? yet in c#, above code results in rem
being long
.
is safe convert rem
int
without loss of data?
int remainder = convert.toint32(rem);
there no overload of modulo operator takes long
, int
, int
converted long
match other operand.
looking @ at lower level, in cpu there no separate instruction calculating modulo, it's 1 of results of division operation. output of operation result of division, , reminder. both same size, result of division has long
, reminder.
as reminder has smaller divisor, can safely cast result int
when divisor comes int
.
Comments
Post a Comment