Page 1 of 1
i can't understand,why???
Posted: Tue Sep 28, 2004 1:44 pm
by Morning
[cpp]
#include <iostream>
using namespace std;
int main()
{
long n = -2147483648;
n *= -1;
cout << n << endl;
}
[/cpp]
my output:
why?
Posted: Wed Sep 29, 2004 6:52 am
by A1
Because the range of Long is:
-2147483648 to 2147483647
so it is not possible ot make it(-2147483648) positive using n*=(-1)!
you can use __int64
Posted: Wed Sep 29, 2004 7:01 am
by Morning
but even int type can handle 2^31,why can't long be larger?
[cpp]
#include <iostream.h>
int main()
{
int i;
i = 2147483648;
cout << i - 1 << endl;
return 0;
}
[/cpp]
Posted: Thu Sep 30, 2004 10:35 am
by A1
sorry standard int type range is
-2^15 to 2^15-1 that is -32768 to 32767
But it is possible that your compiler support more range!

Posted: Thu Sep 30, 2004 12:10 pm
by CDiMa
A1 wrote:sorry standard int type range is
-2^15 to 2^15-1 that is -32768 to 32767
But it is possible that your compiler support more range!

Nope, C++ standards don't impose much on the size of int types.
You can only be certain that a char is exactly one byte, short is at least 2 bytes, int is at least 2 bytes, and long is at least 4 bytes. So it may happen that a certain compiler implements shorts,int and longs as 4 or 8 bytes values.
Ciao!!!
Claudio
Posted: Sat Oct 02, 2004 8:35 am
by A1
Yes CDiMa you are right .
I worte "standard" but should write "in general"
That is "In general int type range is ......"
Posted: Sat Oct 02, 2004 4:40 pm
by Krzysztof Duleba
Then your understanding of "general" is very special. Only some out-of-date compilers, like old Borlands, have sizeof(int) = 2.
Posted: Sun Oct 03, 2004 10:37 am
by A1
Krzysztof Duleba wrote:Then your understanding of "general" is very special. Only some out-of-date compilers, like old Borlands, have sizeof(int) = 2.
As a good programmer,
you should always assume that sizeof (int) is 2 because you have
long for 4 byte number.
(my opinion, you don't need to believe it :|)
Posted: Sun Oct 03, 2004 11:59 am
by Krzysztof Duleba
As a good programmer, you shouldn't assume nothing about int size at all (knowledge that sizeof(int) >= 2 is not based on assuption of course). Have you noticed that almost all configure scripts check that?
But that's not my point, I just referred to "in general". New compilers, working on IA32, have sizeof(int) = 4 and I believe that's the most general case. Other cases are special.
Posted: Sun Oct 03, 2004 12:20 pm
by sohel
Seems that some serious talk is going on.
From what I have heard:
sizeof(char)<= sizeof(int) <= sizeof(long) <= sizeof(long long)..
.. so if we define int as 2 and long as 4, then the condition still holds.
But now the standard for int is 4 and 2 for int is obsolete.
Posted: Sun Oct 03, 2004 1:41 pm
by Krzysztof Duleba
No, standard says that sizeof(int) is implementation-defined. sizeof(int) == 4 is just the most common case nowadays, at least for IA32.