本文介绍了为什么long long int不会像承诺的那样长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要一个> 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。 谢谢 OliverI 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 in12824838841685328664871288825689512810102481120488124096813819281416384815327688166553681713107281826214481952428882010485768212097152822419430482383886088241677721682533554432826671088648271342177288282684354568295368709128301073741824831-21474836488321833283448358836168......and so on.I''m beyond confused - can anyone here help me out? Machine is a Core 2Duo Macbook Pro with OSX10.5, gcc, Xcode.ThanksOliver推荐答案 unsinged long int k = 1<< i; //使用此unsinged long int k = 1<<i; // use this 这种情况​​发生的原因是你只有一半的数字为正数, 另一半用于负数。 看这里: http://www.cplusplus.com/doc/tutorial/variables.html 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-51311 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 reasonto 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 EngineerOptiMetrics, Inc.3115 Professional DriveAnn Arbor, MI 48104-5131 也许你的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不会像承诺的那样长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 09:12