为什么long long int不会像承诺的那样长? [英] Why is long long int not as long as promised??
问题描述
我需要一个> 49位整数类型。试过sizeof(很久很久),说8. 8字节=
64位对吗?但是当我尝试分配超过32位的值时,
就失败了。为了说明:
for(i = 0; i< 64; i ++){
long long int k = 1<< i;
cout<<<<<" \t"<< k<<" \t"<< sizeof(k)<<" ; \ n" ;;
}
结果
1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
......
等等。
我很困惑 - 这里的任何人都可以帮助我吗?机器是Core 2
Duo Macbook Pro,带OSX10.5,gcc,Xcode。
谢谢
Oliver
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
for (i=0; i<64; i++){
long long int k = 1<<i;
cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n";
}
results in
1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
......
and so on.
I''m beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.
Thanks
Oliver
推荐答案
Oliver Graeser写道:
Oliver Graeser wrote:
我需要一个> 49位整数类型。试过sizeof(很久很久),说8. 8字节=
64位对吗?但是当我尝试分配超过32位的值时,
就失败了。为了说明:
for(i = 0; i< 64; i ++){
long long int k = 1<< i;
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
for (i=0; i<64; i++){
long long int k = 1<<i;
unsinged long int k = 1<< i; //使用此
unsinged long int k = 1<<i; // use this
>
cout<< i<<" \t"<< k< <" \t"<< sizeof(k)<<" \ n" ;;
}
>
cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n";
}
这种情况发生的原因是你只有一半的数字为正数,
另一半用于负数。
看这里: http://www.cplusplus.com/doc/tutorial/variables.html
Oliver Graeser写道:
Oliver Graeser wrote:
我需要一个> 49位整数类型。试过sizeof(很久很久),说8. 8字节=
64位对吗?但是当我尝试分配超过32位的值时,
就失败了。为了说明:
for(i = 0; i< 64; i ++){
long long int k = 1<< i;
cout<<<<<" \t"<< k<<" \t"<< sizeof(k)<<" ; \ n" ;;
}
结果
1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8 >
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....
等等。
我很困惑 - 这里的任何人都可以帮助我吗?机器是Core 2
Duo Macbook Pro,带OSX10.5,gcc,Xcode。
谢谢
Oliver
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
for (i=0; i<64; i++){
long long int k = 1<<i;
cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n";
}
results in
1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....
and so on.
I''m beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.
Thanks
Oliver
1是一个int,所以当你向左移动超过32时你就会回绕。
(int)结果非常适合''long long'',所以编译器没有理由抱怨
。试试这个:
const long long int ONE = 1L;
for(i = 0; i< 64; i ++){
long long int k = ONE<< i;
cout<<<<" \t"<< k<<" \t"<< sizeof(k)<< " \ n";
}
-
Al Dunstan,软件工程师
OptiMetrics,Inc。
3115 Professional Drive
Ann Arbor,MI 48104-5131
1 is an int, so when you shift it left by more than 32 you wrap around. The
(int) result fits nicely into a ''long long'', so the compiler has no reason
to complain. Try this:
const long long int ONE=1L;
for (i=0; i<64; i++){
long long int k = ONE << i;
cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n";
}
--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
On Thu,25 2008年9月21:45:13 +0800,Oliver Graeser写道:
On Thu, 25 Sep 2008 21:45:13 +0800, Oliver Graeser wrote:
我需要一个> 49位整数类型。试过sizeof(很久很久),说8. 8字节=
64位对吗?但是当我尝试分配超过32位的值时,
就失败了。为了说明:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
也许你的64位int long long保留正数和负数。
是否有未签名字样。您可以使用哪种类型?
Maybe your 64 bit "int long long" holds positive and negative numbers.
Is there an "unsigned" type you can use?
这篇关于为什么long long int不会像承诺的那样长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!