# size of int, long, long long on codechef

Please forgive me if this question is too silly, but I have to confirm this once for all…

I just want to know the sizes of int, long, long long in bytes, on the codechef platform, according to the Codechef Online Judge… pls help

1 Like

Hello,

The SPOJ online judge uses, for the C/C++ languages, the GCC compiler, so, you can read more about your question here if you are using C. On languages such as Pascal, some compilers define different limits and on others, such as Python, due to big int native support the question becomes irrelevant.

Note, however, that some things might behave differentely on your environment if you use a different compiler or a more specific architecture, like x86 instead of x64, for example. The best advice I can give you on this is to read your language documentation

Best regards,

Bruno

1 Like

1 Like

Funny thing hum?

int isPowerOfTwo (long long x) {

long long powerOfTwo = 1;

while (powerOfTwo < x && powerOfTwo < 1000000000000LL)
powerOfTwo *= 2; return (x == powerOfTwo);
}

And see if it still gives the same error please.

Bruno

1 Like

@kuruma >> Just to tease you, easier methods for checking power of two

int powerOfTwo(int n) {
return !(n&(n-1));
}
2 Likes

Check this out to understand the danger behind logarithms… Ten Ways to Check if an Integer Is a Power Of Two in C - Exploring Binary

and the wonderful article to test power of 2… Ten Ways to Check if an Integer Is a Power Of Two in C - Exploring Binary

1 Like

well, I use C++. I always have a doubt - what is the point in ‘int’ and ‘long’ having the same range, i mean the same size… ? any insight pls…?

Can u explain this? I was just writing a program which has the following function

int isPowerOfTwo (long long x)
{
long long powerOfTwo = 1;

while (powerOfTwo < x && powerOfTwo < 1000000000000)
powerOfTwo *= 2;
return (x == powerOfTwo);
}

but when I submit the solution it says ‘compilation error’ as-
prog.cpp:49: error: integer constant is too large for ‘long’ type
first of all, I did not understand , why it is reported as ‘long’, ofcourse that is another story

THE FOLLOWING CHANGE MADE - USING pow(10,16) worked… and even got AC

int isPowerOfTwo (long long x)
{
long long powerOfTwo = 1;

while (powerOfTwo < x && powerOfTwo < pow(10,16)
powerOfTwo *= 2;
return (x == powerOfTwo);
}

What is the mistake in the 1st function writing 1000000000000 (just 12 zeroes). Is the judge acting strange, or which point am I missing here?

Also, idk about the problems constrains or so, but there are easier ways to see if a number is a power of 2…

Is this any magic… it got AC… I have never known of that kind of notation. Can u explain about it why and,when is it used - 1000000000000LL ?

LL tells the compiler that the constant is indeed a long long one.

1 Like

cant it see by itself ?

@agrp >> Then someone might ask can’t it write programs for itself?

1 Like

I’m a magician, huehuehuhue

Epic Using bit operators is neat!! I was actually thinking on the logarithm of base 2 and checking if it was an integer, but this is even better
(And smarter)

1 Like

@kuruma , well just to warn you, I have learnt that, out of all the possible algorithms to check , if a number is a power of 2, going by ‘the logarithm of base 2’ method should not be preferred at all , as it leads to erroneous results.
As the log(n) function returns a double, it cant give exact answer due to the rounding of digits after the decimal

1 Like

Check this out to understand the danger behind logarithms…
http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/#beware-of-logarithms

and the wonderful article to test power of 2…
http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/

1 Like

@bugkiller, as you rightly mentioned… bit manipulation is the most efficient way to test power of 2

1 Like

@sumanth232, this is awesome… I just learnt the most wonderful way for testing Power of 2…