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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -