Doubling a number - shift left vs. multiplication
Posted
by ToxicAvenger
on Stack Overflow
See other posts from Stack Overflow
or by ToxicAvenger
Published on 2010-06-17T07:38:17Z
Indexed on
2010/06/17
7:43 UTC
Read the original article
Hit count: 151
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.
© Stack Overflow or respective owner