Doubling a number - shift left vs. multiplication
- by ToxicAvenger
What are the differences between
int size = (int)((length * 200L) / 100L); // (1)
and
int size = length << 1; // (2)
(length is int in both cases)
I assume both code snippets want to double the length parameter.
I'd be tempted to use (2) ... so are there any advantages for using (1)? I looked at the edge cases when overflow occurs, and both versions seem to have the same behavior.
Please tell me what am I missing.