I was told by an experienced programmer(spoj,codechef,topcoder etc ) than as a general rule one can use int for values upto 10^9.
What is the general rule for using
signed long int,
unsigned long int,
signed long long int,
unsigned long long int
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on your portability constraints. Formally, an
intis onlyguaranteed to support values in the range
[-32767,32767], so anytimeyou need more, you should use
long. Practically, a lot of programsdon’t need portability to 16 bit machines (which are very rare today),
and on most modern 64 bit platforms,
longwill be 64 bits, which ismore than is needed, and will slow things down (if only because of
poorer locality). If you can assume
intis at least 32 bits, that’swhat you should use pretty much everywhere, with the following
exceptions:
If you’re concerned with the actual bitwise representation, using bitwise
&,|,^and~, or the shifting operators, then you should useunsignedtypes,unsigned intby default, but other sizes if one of the following cases occurs.If you need the modulo arithmetic of
unsigned, use an unsigned type. This is very, very rare, but does occur in things like calculating hashcodes.
If you have really large tables (vectors or any other contiguous representation), you might consider
shortor evensigned char(orunsigned shortorunsigned char, if you’re concerned with the bitwise representation). I would not use plaincharfor anything but characters, since its signedness is implementation defined.If you need to represent values greater than 2^31, then consider
long long. (Often, however, evenlong longwon’t be big enough in such cases, and you’ll need some sort ofBigIntegerclass.)As a general rule, anything but
intshould be the exception.