Ëæ×Å 64 λÌåϵ½á¹¹µÄÆÕ¼°£¬Õë¶Ô 64 λϵͳ׼±¸ºÃÄúµÄ Linux£¿ Èí¼þÒѾ±äµÃ±ÈÒÔÇ°¸üΪÖØÒª¡£ÔÚ±¾ÎÄÖУ¬Äú½«Ñ§Ï°ÈçºÎÔÚ½øÐÐÓï¾äÉùÃ÷¡¢¸³Öµ¡¢Î»ÒÆ¡¢ÀàÐÍת»»¡¢×Ö·û´®¸ñʽ»¯ÒÔ¼°¸ü¶à²Ù×÷ʱ£¬·ÀÖ¹³öÏÖ¿ÉÒÆÖ²ÐÔȱÏÝ¡£
Linux ÊÇ¿ÉÒÔʹÓà 64 λ´¦ÀíÆ÷µÄ¿çƽ̨²Ù×÷ϵͳ֮һ£¬ÏÖÔÚ 64 λµÄϵͳÔÚ·þÎñÆ÷ºÍ×ÀÃæ¶Ë¶¼ÒѾ·Ç³£³£¼ûÁË¡£ºÜ¶à¿ª·¢ÈËÔ±ÏÖÔÚ¶¼ÃæÁÙ×ÅÐèÒª½«×Ô¼ºµÄÓ¦ÓóÌÐò´Ó 32 λ»·¾³ÒÆÖ²µ½ 64 λ»·¾³ÖС£Ëæ×Å Intel£¿ Itanium£¿ ºÍÆäËû 64 λ´¦ÀíÆ÷µÄÒýÈ룬ʹÈí¼þÕë¶Ô 64 λ»·¾³×öºÃ×¼±¸±äµÃÈÕÒæÖØÒªÁË¡£
Óë UNIX£¿ ºÍÆäËûÀà UNIX ²Ù×÷ϵͳһÑù£¬Linux ʹÓÃÁË LP64 ±ê×¼£¬ÆäÖÐÖ¸ÕëºÍ³¤ÕûÊý¶¼ÊÇ 64 λµÄ£¬¶øÆÕͨµÄÕûÊýÔòÒÀÈ»ÊÇ 32 λµÄ¡£¾¡¹ÜÓÐЩ¸ß¼¶ÓïÑÔ²¢²»»áÊܵ½ÕâÖÖÀàÐÍ´óС²»Í¬µÄÓ°Ï죬µ«ÊÇÁíÍâһЩÓïÑÔ£¨ÀýÈç C ÓïÑÔ£©È´µÄÈ·»áÊܵ½ÕâÖÖÓ°Ïì¡£
½«Ó¦ÓóÌÐò´Ó 32 λϵͳÒÆÖ²µ½ 64 λϵͳÉϵŤ×÷¿ÉÄÜ»á·Ç³£¼òµ¥£¬Ò²¿ÉÄÜ»á·Ç³£À§ÄÑ£¬ÕâÈ¡¾öÓÚÕâЩӦÓóÌÐòÊÇÈçºÎ±àдºÍά»¤µÄ¡£ºÜ¶àËöËéµÄÎÊÌⶼ¿ÉÄܵ¼Ö²úÉúÎÊÌ⣬¼´Ê¹ÔÚÒ»¸ö±àдµÃ·Ç³£ºÃµÄ¸ß¶È¿ÉÒÆÖ²µÄÓ¦ÓóÌÐòÖÐÒ²ÊÇÈç´Ë£¬Òò´Ë±¾ÎĽ«¶ÔÕâЩÎÊÌâ½øÐйéÄÉ×ܽᣬ²¢¸ø³ö½â¾öÕâЩÎÊÌâµÄһЩ·½·¨½¨Òé¡£
64 λµÄÓŵã
32 λƽ̨ÓкܶàÏÞÖÆ£¬ÕâЩÏÞÖÆÕýÔÚ×è°´óÐÍÓ¦ÓóÌÐò£¨ÀýÈçÊý¾Ý¿â£©¿ª·¢ÈËÔ±µÄ¹¤×÷½øÕ¹£¬ÓÈÆä¶ÔÄÇЩϣÍû³ä·ÖÀûÓüÆËã»úÓ²¼þÓŵãµÄ¿ª·¢ÈËÔ±À´Ëµ¸üÊÇÈç´Ë¡£¿Æѧ¼ÆËãͨ³£ÒªÒÀÀµÓÚ¸¡µã¼ÆË㣬¶øÓÐЩӦÓóÌÐò£¨ÀýÈç½ðÈÚ¼ÆË㣩ÔòÐèÒªÒ»¸ö±È½ÏÏÁÕµÄÊý×Ö·¶Î§£¬µ«ÊÇÈ´ÒªÇó¸ü¸ßµÄ¾«¶È£¬Æ侫¶È¸ßÓÚ¸¡µãÊýËùÌṩµÄ¾«¶È¡£64 λÊýѧÔËËãÌṩÁËÕâÖÖ¸ü¸ß¾«¶ÈµÄ¶¨µãÊýѧ¼ÆË㣬ͬʱ»¹ÌṩÁË×ã¹»µÄÊý×Ö·¶Î§¡£ÏÖÔÚÔÚ¼ÆËã»úÒµ½çÖÐÓкܶà¹ØÓÚ 32 λµØÖ·¿Õ¼äËù±íʾµÄµØÖ·¿Õ¼äµÄÌÖÂÛ¡£32 λָÕëÖ»ÄÜÑ°Ö· 4GB µÄÐéÄâµØÖ·¿Õ¼ä¡£ÎÒÃÇ¿ÉÒÔ¿Ë·þÕâÖÖÏÞÖÆ£¬µ«ÊÇÓ¦ÓóÌÐò¿ª·¢¾Í±äµÃ·Ç³£¸´ÔÓÁË£¬ÆäÐÔÄÜÒ²»áÏÔÖø½µµÍ¡£
ÔÚÓïÑÔʵÏÖ·½Ã棬ĿǰµÄ C ÓïÑÔ±ê×¼ÒªÇó ¡°long long¡± Êý¾ÝÀàÐÍÖÁÉÙÊÇ 64 λµÄ¡£È»¶ø£¬ÆäʵÏÖ¿ÉÄܻὫÆ䶨ÒåΪ¸ü´ó¡£
ÁíÍâÒ»¸öÐèÒª¸Ä½øµÄµØ·½ÊÇÈÕÆÚ¡£ÔÚ Linux ÖУ¬ÈÕÆÚÊÇʹÓà 32 λÕûÊýÀ´±íʾµÄ£¬¸ÃÖµËù±íʾµÄÊÇ´Ó 1970 Äê 1 Ô 1 ÈÕÖÁ½ñËù¾¹ýµÄÃëÊý¡£ÕâÔÚ 2038 Äê¾Í»áʧЧ¡£µ«ÊÇÔÚ 64 λµÄϵͳÖУ¬ÈÕÆÚÊÇʹÓÃÓзûºÅµÄ 64 λÕûÊý±íʾµÄ£¬Õâ¿ÉÒÔ¼«´óµØÀ©³äÆä¿ÉÓ÷¶Î§¡£
×ÜÖ®£¬64 λ¾ßÓÐÒÔÏÂÓŵ㣺
1. 64 λµÄÓ¦ÓóÌÐò¿ÉÒÔÖ±½Ó·ÃÎÊ 4EB µÄÐéÄâÄڴ棬Intel Itanium ´¦ÀíÆ÷ÌṩÁËÁ¬ÐøµÄÏßÐÔµØÖ·¿Õ¼ä¡£
2. 64 λµÄ Linux ÔÊÐíÎļþ´óС×î´ó´ïµ½ 4 EB£¨2 µÄ 63 ´ÎÃÝ£©£¬ÆäÖØÒªµÄÓŵãÖ®Ò»¾ÍÊÇ¿ÉÒÔ´¦Àí¶Ô´óÐÍÊý¾Ý¿âµÄ·ÃÎÊ¡£
Linux 64 λÌåϵ½á¹¹
²»ÐÒµÄÊÇ£¬C ±à³ÌÓïÑÔ²¢Ã»ÓÐÌṩһÖÖ»úÖÆÀ´Ìí¼ÓеĻù±¾Êý¾ÝÀàÐÍ¡£Òò´Ë£¬Ìṩ 64 λµÄÑ°Ö·ºÍÕûÊýÔËËãÄÜÁ¦±ØÐëÒªÐÞ¸ÄÏÖÓÐÊý¾ÝÀàÐ͵İ󶨻òÓ³É䣬»òÕßÏò C ÓïÑÔÖÐÌí¼ÓеÄÊý¾ÝÀàÐÍ¡£
±í 1. 32 λºÍ 64 λÊý¾ÝÄ£ÐÍ ILP32 LP64 LLP64 ILP64
char 8 8 8 8
short 16 16 16 16
int 32 32 32 64
long 32 64 32 64
long long 64 64 64 64
Ö¸Õë 32 64 64 64
Õâ 3 ¸ö 64 λģÐÍ£¨LP64¡¢LLP64 ºÍ ILP64£©Ö®¼äµÄÇø±ðÔÚÓڷǸ¡µãÊý¾ÝÀàÐÍ¡£µ±Ò»¸ö»ò¶à¸ö C Êý¾ÝÀàÐ͵Ŀí¶È´ÓÒ»ÖÖÄ£Ðͱ任³ÉÁíÍâÒ»ÖÖÄ£ÐÍʱ£¬Ó¦ÓóÌÐò¿ÉÄÜ»áÊܵ½ºÜ¶à·½ÃæµÄÓ°Ïì¡£ÕâЩӰÏìÖ÷Òª¿ÉÒÔ·ÖΪÁ½Àࣺ
Êý¾Ý¶ÔÏóµÄ´óС¡£±àÒëÆ÷°´ÕÕ×ÔÈ»±ß½ç¶ÔÊý¾ÝÀàÐͽøÐжÔÆ룻»»¶øÑÔÖ®£¬32 λµÄÊý¾ÝÀàÐÍÔÚ 64 λϵͳÉÏÒª°´ÕÕ 32 λ±ß½ç½øÐжÔÆ룬¶ø 64 λµÄÊý¾ÝÀàÐÍÔÚ 64 λϵͳÉÏÔòÒª°´ÕÕ 64 λ±ß½ç½øÐжÔÆë¡£ÕâÒâζ×ÅÖîÈç½á¹¹»òÁªºÏÖ®ÀàµÄÊý¾Ý¶ÔÏóµÄ´óСÔÚ 32 λºÍ 64 λϵͳÉÏÊDz»Í¬µÄ¡£
»ù±¾Êý¾ÝÀàÐ͵ĴóС¡£Í¨³£¹ØÓÚ»ù±¾Êý¾ÝÀàÐÍÖ®¼ä¹ØϵµÄ¼ÙÉèÔÚ 64 λÊý¾ÝÄ£ÐÍÉ϶¼ÒѾÎÞЧÁË¡£ÒÀÀµÓÚÕâЩ¹ØϵµÄÓ¦ÓóÌÐòÔÚ 64 λƽ̨ÉϱàÒëÒ²»áʧ°Ü¡£ÀýÈ磬sizeof £¨int£© = sizeof £¨long£© = sizeof £¨pointer£© µÄ¼ÙÉè¶ÔÓÚ ILP32 Êý¾ÝÄ£ÐÍÓÐЧ£¬µ«ÊǶÔÓÚÆäËûÊý¾ÝÄ£Ð;ÍÎÞЧÁË¡£
×ÜÖ®£¬±àÒëÆ÷Òª°´ÕÕ×ÔÈ»±ß½ç¶ÔÊý¾ÝÀàÐͽøÐжÔÆ룬ÕâÒâζ×űàÒëÆ÷»á½øÐÐ ¡°Ìî³ä¡±£¬´Ó¶øÇ¿ÖƽøÐÐÕâÖÖ·½Ê½µÄ¶ÔÆ룬¾ÍÏñÊÇÔÚ C ½á¹¹ºÍÁªºÏÖÐËù×öµÄÒ»Ñù¡£½á¹¹»òÁªºÏµÄ³ÉÔ±ÊǸù¾Ý×î¿íµÄ³ÉÔ±½øÐжÔÆëµÄ¡£Çåµ¥ 1 ¶ÔÕâ¸ö½á¹¹½øÐÐÁ˽âÊÍ¡£
Çåµ¥ 1. C ½á¹¹
struct test {
int i1;
double d;
int i2;
long l;
}
±í 2 ¸ø³öÁËÕâ¸ö½á¹¹ÖÐÿ¸ö³ÉÔ±µÄ´óС£¬ÒÔ¼°Õâ¸ö½á¹¹ÔÚ 32 λϵͳºÍ 64 λϵͳÉϵĴóС¡£
±í 2. ½á¹¹ºÍ½á¹¹³ÉÔ±µÄ´óС
½á¹¹³ÉÔ± ÔÚ 32 λϵͳÉϵĴóС ÔÚ 64 λϵͳÉϵĴóС
struct test {
int i1; 32 λ 32 λ
32 λÌî³ä
double d; 64 λ 64 λ
int i2; 32 λ 32 λ
32 λÌî³ä
long l; 32 λ 64 λ
}; ½á¹¹´óСΪ 20 ×Ö½Ú ½á¹¹´óСΪ 32 ×Ö½Ú
×¢Ò⣬ÔÚÒ»¸ö 32 λµÄϵͳÉÏ£¬±àÒëÆ÷¿ÉÄܲ¢Ã»ÓжԱäÁ¿ d ½øÐжÔÆ룬¾¡¹ÜËüÊÇÒ»¸ö 64 λµÄ¶ÔÏó£¬ÕâÊÇÒòΪӲ¼þ»á½«Æäµ±×÷Á½¸ö 32 λµÄ¶ÔÏó½øÐд¦Àí¡£È»¶ø£¬64 λµÄϵͳ»á¶Ô d ºÍ l ¶¼½øÐжÔÆ룬ÕâÑù»áÌí¼ÓÁ½¸ö 4 ×Ö½ÚµÄÌî³ä¡£
´Ó 32 λϵͳÒÆÖ²µ½ 64 λϵͳ
±¾½Ú½éÉÜÈçºÎ½â¾öһЩ³£¼ûµÄÎÊÌ⣺
ÉùÃ÷±í´ïʽ¸³ÖµÊý×Ö³£ÊýEndianismÀàÐͶ¨ÒåλÒÆ×Ö·û´®¸ñʽ»¯º¯Êý²ÎÊý
ÉùÃ÷
ÒªÏëÈÃÄúµÄ´úÂëÔÚ 32 λºÍ 64 λϵͳÉ϶¼¿ÉÒÔ¹¤×÷£¬Çë×¢ÒâÒÔÏÂÓйØÉùÃ÷µÄÓ÷¨£º
¸ù¾ÝÐèÒªÊʵ±µØʹÓà ¡°L¡± »ò ¡°U¡± À´ÉùÃ÷ÕûÐͳ£Á¿¡£
È·±£Ê¹ÓÃÎÞ·ûºÅÕûÊýÀ´·ÀÖ¹·ûºÅÀ©Õ¹µÄÎÊÌâ¡£
Èç¹ûÓÐЩ±äÁ¿ÔÚÕâÁ½¸öƽ̨É϶¼ÐèÒªÊÇ 32 λµÄ£¬Ç뽫ÆäÀàÐͶ¨ÒåΪ int.Èç¹ûÓÐЩ±äÁ¿ÔÚ 32 λϵͳÉÏÊÇ 32 λµÄ£¬ÔÚ 64 λϵͳÉÏÊÇ 64 λµÄ£¬Ç뽫ÆäÀàÐͶ¨ÒåΪ long.ΪÁ˶ÔÆëºÍÐÔÄܵÄÐèÒª£¬Ç뽫Êý×Ö±äÁ¿ÉùÃ÷Ϊ int »ò long ÀàÐÍ¡£²»ÒªÊÔͼʹÓà char »ò short ÀàÐÍÀ´±£´æ×Ö½Ú¡£
½«×Ö·ûÖ¸ÕëºÍ×Ö·û×Ö½ÚÉùÃ÷ΪÎÞ·ûºÅÀàÐ͵ģ¬ÕâÑù¿ÉÒÔ·ÀÖ¹ 8 λ×Ö·ûµÄ·ûºÅÀ©Õ¹ÎÊÌâ¡£
±í´ïʽ
ÔÚ C/C++ ÖУ¬±í´ïʽÊÇ»ùÓÚ½áºÏÂÉ¡¢²Ù×÷·ûµÄÓÅÏȼ¶ºÍÒ»×éÊýѧ¼ÆËã¹æÔòµÄ¡£ÒªÏëÈñí´ïʽÔÚ 32 λºÍ 64 λϵͳÉ϶¼¿ÉÒÔÕýÈ·¹¤×÷£¬Çë×¢ÒâÒÔϹæÔò£º
Á½¸öÓзûºÅÕûÊýÏà¼ÓµÄ½á¹ûÊÇÒ»¸öÓзûºÅÕûÊý¡£
int ºÍ long ÀàÐ͵ÄÁ½¸öÊýÏà¼Ó£¬½á¹ûÊÇÒ»¸ö long ÀàÐ͵ÄÊý¡£
Èç¹ûÒ»¸ö²Ù×÷ÊýÊÇÎÞ·ûºÅÕûÊý£¬ÁíÍâÒ»¸ö²Ù×÷ÊýÊÇÓзûºÅÕûÊý£¬ÄÇô±í´ïʽµÄ½á¹û¾ÍÊÇÎÞ·ûºÅÕûÊý¡£
int ºÍ doubule ÀàÐ͵ÄÁ½¸öÊýÏà¼Ó£¬½á¹ûÊÇÒ»¸ö double ÀàÐ͵ÄÊý¡£´Ë´¦ int ÀàÐ͵ÄÊýÔÚÖ´Ðмӷ¨ÔËËã֮ǰת»»³É double ÀàÐÍ¡£
¸³Öµ
ÓÉÓÚÖ¸Õë¡¢int ºÍ long ÔÚ 64 λϵͳÉÏ´óС²»ÔÙÏàͬÁË£¬Òò´Ë¸ù¾ÝÕâЩ±äÁ¿ÊÇÈçºÎ¸³ÖµºÍÔÚÓ¦ÓóÌÐòÖÐʹÓõģ¬¿ÉÄÜ»á³öÏÖÎÊÌâ¡£ÏÂÃæÊÇÓйظ³ÖµµÄһЩ¼¼ÇÉ£º
²»Òª½»»»Ê¹Óà int ºÍ long ÀàÐÍ£¬ÒòΪÕâ¿ÉÄܻᵼÖ¸ßλÊý×Ö±»½Ø¶Ï¡£ÀýÈ磬²»Òª×öÏÂÃæµÄÊÂÇ飺
int i;
long l;
i = l;
²»ÒªÊ¹Óà int ÀàÐÍÀ´´æ´¢Ö¸Õë¡£ÏÂÃæÕâ¸öÀý×ÓÔÚ 32 λϵͳÉÏ¿ÉÒԺܺõع¤×÷£¬µ«ÊÇÔÚ 64 λϵͳÉÏ»áʧ°Ü£¬ÕâÊÇÒòΪ 32 λÕûÊýÎÞ·¨´æ·Å 64 λµÄÖ¸Õë¡£ÀýÈ磬²»Òª×öÏÂÃæµÄÊÂÇ飺
unsigned int i, *ptr;
i = (unsigned) ptr;
²»ÒªÊ¹ÓÃÖ¸ÕëÀ´´æ·Å int ÀàÐ͵ÄÖµ¡£ÀýÈ磬²»Òª×öÏÂÃæµÄÊÂÇ飻
int *ptr;
int i;
ptr = (int *) i;
Èç¹ûÔÚ±í´ïʽÖлìºÏʹÓÃÎÞ·ûºÅºÍÓзûºÅµÄ 32 λÕûÊý£¬²¢½«Æ丳ֵ¸øÒ»¸öÓзûºÅµÄ long ÀàÐÍ£¬ÄÇô½«ÆäÖÐÒ»¸ö²Ù×÷Êýת»»³É 64 λµÄÀàÐÍ¡£Õâ»áµ¼ÖÂÆäËû²Ù×÷ÊýÒ²±»×ª»»³É 64 λµÄÀàÐÍ£¬ÕâÑùÔÚ¶Ô±í´ïʽ½øÐи³ÖµÊ±¾Í²»ÐèÒªÔÙ½øÐÐת»»ÁË¡£ÁíÍâÒ»ÖÖ½â¾ö·½°¸ÊǶÔÕû¸ö±í´ïʽ½øÐÐת»»£¬ÕâÑù¾Í¿ÉÒÔÔÚ¸³ÖµÊ±½øÐзûºÅÀ©Õ¹¡£ÀýÈ磬¿¼ÂÇÏÂÃæÕâÖÖÓ÷¨¿ÉÄÜ»á³öÏÖµÄÎÊÌ⣺
long n;
int i = -2;
unsigned k = 1;
n = i + k;
´ÓÊýѧ¼ÆËãÉÏÀ´Ëµ£¬ÉÏÃæÕâ¸öºÚÌåÏÔʾµÄ±í´ïʽµÄ½á¹ûÓ¦¸ÃÊÇ -1 ¡£µ«ÊÇÓÉÓÚ±í´ïʽÊÇÎÞ·ûºÅµÄ£¬Òò´Ë²»»á½øÐзûºÅÀ©Õ¹¡£½â¾ö·½°¸Êǽ«Ò»¸ö²Ù×÷Êýת»»³É 64 λÀàÐÍ£¨ÏÂÃæµÄµÚÒ»ÐоÍÊÇÕâÑù£©£¬»òÕ߶ÔÕû¸ö±í´ïʽ½øÐÐת»»£¨ÏÂÃæµÚ¶þÐУ©£º
n = (long) i + k;
n = (int) (i + k);
Êý×Ö³£Á¿
16 ½øÖƵij£Á¿Í¨³£¶¼ÓÃ×÷ÑÚÂë»òÌØÊâλµÄÖµ¡£Èç¹ûÒ»¸öûÓкó׺µÄ 16 ½øÖƵij£Á¿ÊÇ 32 λµÄ£¬²¢ÇÒÆä¸ßλ±»ÖÃλÁË£¬ÄÇôËü¾Í¿ÉÒÔ×÷ΪÎÞ·ûºÅÕûÐͽøÐж¨Òå¡£
ÀýÈ磬³£Êý OxFFFFFFFFL ÊÇÒ»¸öÓзûºÅµÄ long ÀàÐÍ¡£ÔÚ 32 λϵͳÉÏ£¬Õâ»á½«ËùÓÐλ¶¼ÖÃλ£¨Ã¿Î»È«Îª 1£©£¬µ«ÊÇÔÚ 64 λϵͳÉÏ£¬Ö»ÓÐµÍ 32 λ±»ÖÃλÁË£¬½á¹ûÊÇÕâ¸öÖµÊÇ 0x00000000FFFFFFFF.
Èç¹ûÎÒÃÇÏ£ÍûËùÓÐλȫ²¿ÖÃ룬ÄÇôһÖÖ¿ÉÒÆÖ²µÄ·½·¨ÊǶ¨ÒåÒ»¸öÓзûºÅµÄ³£Êý£¬ÆäֵΪ -1.Õâ»á½«ËùÓÐλȫ²¿ÖÃ룬ÒòΪËü²ÉÓÃÁ˶þ½øÖƲ¹ÂëËã·¨¡£
long x = -1L;
¿ÉÄܲúÉúµÄÁíÍâÒ»¸öÎÊÌâÊÇ×î¸ßλµÄÉèÖá£ÔÚ 32 λϵͳÉÏ£¬ÎÒÃÇʹÓõÄÊdz£Á¿ 0x80000000¡£µ«ÊÇ¿ÉÒÆÖ²ÐÔ¸üºÃµÄ·½·¨ÊÇʹÓÃÒ»¸öλÒƱí´ïʽ£º
1L << ((sizeof(long) * 8) - 1);
Endianism
Endianism ÊÇÖ¸ÓÃÀ´´æ´¢Êý¾ÝµÄ·½·¨£¬Ëü¶¨ÒåÁËÕûÊýºÍ¸¡µãÊý¾ÝÀàÐÍÖÐÊÇÈçºÎ¶Ô×Ö½Ú½øÐÐÑ°Ö·µÄ¡£
Little-endian Êǽ«µÍλ×Ö½Ú´æ´¢ÔÚÄÚ´æµÄµÍµØÖ·ÖУ¬½«¸ßλ×Ö½Ú´æ´¢ÔÚÄÚ´æµÄ¸ßµØÖ·ÖС£
Big-endian Êǽ«¸ßλ×Ö½Ú´æ´¢ÔÚÄÚ´æµÄµÍµØÖ·ÖУ¬½«µÍλ×Ö½Ú´æ´¢ÔÚÄÚ´æµÄ¸ßµØÖ·ÖС£
±í 3 ¸ø³öÁËÒ»¸ö 64 볤ÕûÊýµÄ²¼¾ÖʾÀý¡£
±í 3. 64 λ long int ÀàÐ͵IJ¼¾Ö
µÍµØÖ· ¸ßµØÖ·
Little endian Byte 0 Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6 Byte 7
Big endian Byte 7 Byte 6 Byte 5 Byte 4 Byte 3 Byte 2 Byte 1 Byte 0
ÀýÈ磬32 λµÄ×Ö 0x12345678 ÔÚ big endian »úÆ÷ÉϵIJ¼¾ÖÈçÏ£º
±í 4. 0x12345678 ÔÚ big-endian ϵͳÉϵIJ¼¾Ö
ÄÚ´æÆ«ÒÆÁ¿ 0 1 2 3
ÄÚ´æÄÚÈÝ 0x12 0x34 0x56 0x78
Èç¹û½« 0x12345678 µ±×÷Á½¸ö°ë×ÖÀ´¿´´ý£¬·Ö±ðÊÇ 0x1234 ºÍ 0x5678£¬ÄÇô¾Í»á¿´µ½ÔÚ big endian »úÆ÷ÉÏÊÇÏÂÃæµÄÇé¿ö£º
±í 5. 0x12345678 ÔÚ big-endian ϵͳÉϵ±×÷Á½¸ö°ë×ÖÀ´¿´´ýµÄÇé¿ö
ÄÚ´æÆ«ÒÆÁ¿ 0 2
ÄÚ´æÄÚÈÝ 0x1234 0x5678
È»¶ø£¬ÔÚ little endian »úÆ÷ÉÏ£¬×Ö 0x12345678 µÄ²¼¾ÖÈçÏÂËùʾ£º
±í 6. 0x12345678 ÔÚ little-endian ϵͳÉϵIJ¼¾Ö
ÄÚ´æÆ«ÒÆÁ¿ 0 1 2 3
ÄÚ´æÄÚÈÝ 0x78 0x56 0x34 0x12
ÀàËƵأ¬Á½¸ö°ë×Ö 0x1234 ºÍ 0x5678 ÈçÏÂËùʾ£º
±í 7. 0x12345678 ÔÚ little-endian ϵͳÉÏ×÷ΪÁ½¸ö°ë×Ö¿´µ½µÄÇé¿ö
ÄÚ´æÆ«ÒÆÁ¿ 0 2
ÄÚ´æÄÚÈÝ 0x3412 0x7856
ÏÂÃæÕâ¸öÀý×Ó½âÊÍÁË big endian ºÍ little endian »úÆ÷ÉÏ×Ö½Ú˳ÐòÖ®¼äµÄÇø±ð¡£
ÏÂÃæµÄ C ³ÌÐòÔÚһ̨ big endian »úÆ÷ÉϽøÐбàÒëºÍÔËÐÐʱ»á´òÓ¡ ¡°Big endian¡±£¬ÔÚһ̨ little endian »úÆ÷ÉϽøÐбàÒëºÍÔËÐÐʱ»á´òÓ¡ ¡°Little endian¡±¡£
Çåµ¥ 2. big endian Óë little endian
#include
main () {
int i = 0x12345678;
if (*(char *)&i == 0x12)
printf ("Big endian\n");
else if (*(char *)&i == 0x78)
printf ("Little endian\n");
}
Endianism ÔÚÒÔÏÂÇé¿öÖзdz£ÖØÒª£º
ʹÓÃλÑÚÂëʱ
¶ÔÏóµÄ¼ä½ÓÖ¸ÕëµØÖ·²¿·Ö
ÔÚ C ºÍ C++ ÖÐÓÐλÓòÀ´°ïÖú´¦Àí endian µÄÎÊÌâ¡£ÎÒ½¨ÒéʹÓÃλÓò£¬¶ø²»ÒªÊ¹ÓÃÑÚÂëÓò»ò 16 ½øÖƵij£Á¿¡£Óм¸¸öº¯Êý¿ÉÒÔÓÃÀ´½« 16 λºÍ 32 λÊý¾Ý´Ó ¡°Ö÷»ú×Ö½Ú˳Ðò¡± ת»»³É ¡°ÍøÂç×Ö½Ú˳Ðò¡±¡£ÀýÈ磬htonl £¨3£©¡¢ntohl £¨3£© ÓÃÀ´×ª»» 32 λÕûÊý¡£ÀàËƵأ¬htons £¨3£©¡¢ntohs £¨3£© ÓÃÀ´×ª»» 16 λÕûÊý¡£È»¶ø£¬¶ÔÓÚ 64 λÕûÊýÀ´Ëµ£¬²¢Ã»Óбê×¼µÄº¯Êý¼¯¡£µ«ÊÇÔÚ big endian ºÍ little endian ϵͳÉÏ£¬Linux ¶¼ÌṩÁËÏÂÃæµÄ¼¸¸öºê£º
bswap_16
bswap_32
bswap_64
ÀàÐͶ¨Òå
½¨ÒéÄú²»ÒªÊ¹Óà C/C++ ÖÐÄÇЩÔÚ 64 λϵͳÉÏ»á¸Ä±ä´óСµÄÊý¾ÝÀàÐÍÀ´±àдӦÓóÌÐò£¬¶øÊÇʹÓÃһЩÀàÐͶ¨Òå»òºêÀ´ÏÔʽµØ˵Ã÷±äÁ¿ÖÐËù°üº¬µÄÊý¾ÝµÄ´óСºÍÀàÐÍ¡£ÓÐЩ¶¨Òå¿ÉÒÔʹ´úÂëµÄ¿ÉÒÆÖ²ÐÔ¸üºÃ¡£
ptrdiff_t£º
ÕâÊÇÒ»¸öÓзûºÅÕûÐÍ£¬ÊÇÁ½¸öÖ¸ÕëÏà¼õºóµÄ½á¹û¡£
size_t£º
ÕâÊÇÒ»¸öÎÞ·ûºÅÕûÐÍ£¬ÊÇÖ´ÐÐ sizeof ²Ù×÷µÄ½á¹û¡£ÕâÔÚÏòһЩº¯Êý£¨ÀýÈç malloc £¨3£©£©´«µÝ²ÎÊýʱʹÓã¬Ò²¿ÉÒÔ´ÓһЩº¯Êý£¨±ÈÈç fred £¨2£©£©Öзµ»Ø¡£
int32_t¡¢uint32_t µÈ£º
¶¨Òå¾ßÓÐÔ¤¶¨Òå¿í¶ÈµÄÕûÐÍ¡£
intptr_t ºÍ uintptr_t£º
¶¨ÒåÕûÐÍÀàÐÍ£¬ÈκÎÓÐЧָÕ붼¿ÉÒÔת»»³ÉÕâ¸öÀàÐÍ¡£
Àý 1£º
ÔÚÏÂÃæÕâÌõÓï¾äÖУ¬ÔÚ¶Ô bufferSize ½øÐи³ÖµÊ±£¬´Ó sizeof ·µ»ØµÄ 64 λֵ±»½Ø¶Ï³ÉÁË 32 λ¡£
int bufferSize = £¨int£© sizeof £¨something£©£»
½â¾ö·½°¸ÊÇʹÓà size_t ¶Ô·µ»ØÖµ½øÐÐÀàÐÍת»»£¬²¢½«Æ丳¸øÉùÃ÷Ϊ size_t ÀàÐ굀 bufferSize£¬ÈçÏÂËùʾ£º
size_t bufferSize = £¨size_t£© sizeof £¨something£©£»
Àý 2£º
ÔÚ 32 λϵͳÉÏ£¬int ºÍ long ´óСÏàͬ¡£ÓÉÓÚÕâÒ»µã£¬ÓÐЩ¿ª·¢ÈËÔ±»á½»»»Ê¹ÓÃÕâÁ½ÖÖÀàÐÍ¡£Õâ¿ÉÄܻᵼÖÂÖ¸Õë±»¸³Öµ¸ø int ÀàÐÍ£¬»òÕß·´Ö®¡£µ«ÊÇÔÚ 64 λµÄϵͳÉÏ£¬½«Ö¸Õ븳ֵ¸ø int ÀàÐͻᵼÖÂ½Ø¶Ï¸ß 32 λµÄÖµ¡£
½â¾ö·½°¸Êǽ«Ö¸Õë×÷ΪָÕëÀàÐÍ»òΪ´Ë¶ø¶¨ÒåµÄÌØÊâÀàÐͽøÐд洢£¬ÀýÈç intptr_t ºÍ uintptr_t.
λÒÆ
ÎÞÀàÐ͵ÄÕûÊý³£Á¿¾ÍÊÇ £¨unsigned£© int ÀàÐ͵ġ£Õâ¿ÉÄܻᵼÖÂÔÚλÒÆʱ³öÏÖ±»½Ø¶ÏµÄÎÊÌâ¡£
ÀýÈ磬ÔÚÏÂÃæµÄ´úÂëÖУ¬a µÄ×î´óÖµ¿ÉÒÔÊÇ 31.ÕâÊÇÒòΪ 1 << a ÊÇ int ÀàÐ͵ġ£
long t = 1 << a£»
ÒªÔÚ 64 λϵͳÉϽøÐÐλÒÆ£¬Ó¦¸ÃʹÓà 1L£¬ÈçÏÂËùʾ£º
long t = 1L << a£»
×Ö·û´®¸ñʽ»¯
º¯Êý printf £¨3£© ¼°ÆäÏà¹Øº¯Êý¶¼¿ÉÄܳÉΪÎÊÌâµÄ¸ùÔ´¡£ÀýÈ磬ÔÚ 32 λϵͳÉÏ£¬Ê¹Óà %d À´´òÓ¡ int »ò long ÀàÐ͵ÄÖµ¶¼¿ÉÒÔ£¬µ«ÊÇÔÚ 64 λƽ̨ÉÏ£¬Õâ»áµ¼Ö½« long ÀàÐ͵ÄÖµ½Ø¶Ï³ÉµÍ 32 λµÄÖµ¡£¶ÔÓÚ long ÀàÐ͵ıäÁ¿À´Ëµ£¬ÕýÈ·µÄÓ÷¨ÊÇ %ld.
ÀàËƵأ¬µ±Ò»¸öСÕûÊý£¨char¡¢short¡¢int£©±»´«µÝ¸ø printf £¨3£© ʱ£¬Ëü»áÀ©Õ¹³É 64 λµÄ£¬·ûºÅ»áÊʵ±µØ½øÐÐÀ©Õ¹¡£ÔÚÏÂÃæµÄÀý×ÓÖУ¬printf £¨3£© ¼ÙÉèÖ¸ÕëÊÇ 32 λµÄ¡£
char *ptr = &something£»printf £¨%x\n"£¬ ptr£©£»
ÉÏÃæµÄ´úÂëÔÚ 64 λϵͳÉÏ»áʧ°Ü£¬ËüÖ»»áÏÔʾµÍ 4 ×Ö½ÚµÄÄÚÈÝ¡£
Õâ¸öÎÊÌâµÄ½â¾ö·½°¸ÊÇʹÓà %p£¬ÈçÏÂËùʾ£»ÕâÔÚ 32 λºÍ 64 λϵͳÉ϶¼¿ÉÒԺܺõع¤×÷£º
char *ptr = &something£»printf £¨%p\n"£¬ ptr£©£»
º¯Êý²ÎÊý
ÔÚÏòº¯Êý´«µÝ²ÎÊýʱÐèÒª¼Çס¼¸¼þÊÂÇ飺
ÔÚ²ÎÊýµÄÊý¾ÝÀàÐÍÊÇÓɺ¯ÊýÔÐͶ¨ÒåµÄÇé¿öÖУ¬²ÎÊýÓ¦¸Ã¸ù¾Ý±ê×¼¹æÔòת»»³ÉÕâÖÖÀàÐÍ¡£
ÔÚ²ÎÊýÀàÐÍûÓÐÖ¸¶¨µÄÇé¿öÖУ¬²ÎÊý»á±»×ª»»³É¸ü´óµÄÀàÐÍ¡£
ÔÚ 64 λϵͳÉÏ£¬ÕûÐͱ»×ª»»³É 64 λµÄÕûÐÍÖµ£¬µ¥¾«¶ÈµÄ¸¡µãÀàÐͱ»×ª»»³ÉË«¾«¶ÈµÄ¸¡µãÀàÐÍ¡£
Èç¹û·µ»ØֵûÓÐÖ¸¶¨£¬ÄÇôº¯ÊýµÄȱʡ·µ»ØÖµÊÇ int ÀàÐ͵ġ£
ÔÚ½«ÓзûºÅÕûÐͺÍÎÞ·ûºÅÕûÐ͵ĺÍ×÷Ϊ long ÀàÐÍ´«µÝʱ¾Í»á³öÏÖÎÊÌâ¡£¿¼ÂÇÏÂÃæµÄÇé¿ö£º
Çåµ¥ 3. ½«ÓзûºÅÕûÐͺÍÎÞ·ûºÅÕûÐ͵ĺÍ×÷Ϊ long ÀàÐÍ´«µÝ
long function (long l);
int main () {
int i = -2;
unsigned k = 1U;
long n = function (i + k);
}
ÉÏÃæÕâ¶Î´úÂëÔÚ 64 λϵͳÉÏ»áʧ°Ü£¬ÒòΪ±í´ïʽ (i + k) ÊÇÒ»¸öÎÞ·ûºÅµÄ 32 λ±í´ïʽ£¬ÔÚ½«Æäת»»³É long ÀàÐÍʱ£¬·ûºÅ²¢Ã»Óеõ½À©Õ¹¡£½â¾ö·½°¸Êǽ«Ò»¸ö²Ù×÷ÊýÇ¿ÖÆת»»³É 64 λµÄÀàÐÍ¡£
ÔÚ»ùÓڼĴæÆ÷µÄϵͳÉÏ»¹ÓÐÒ»¸öÎÊÌ⣺ϵͳ²ÉÓüĴæÆ÷¶ø²»ÊǶÑÕ»À´Ïòº¯Êý´«µÝ²ÎÊý¡£¿¼ÂÇÏÂÃæµÄÀý×Ó£º
float f = 1.25;
printf ("The hex value of %f is %x", f, f);
ÔÚ»ùÓÚ¶ÑÕ»µÄϵͳÖУ¬Õâ»á´òÓ¡¶ÔÓ¦µÄ 16 ½øÖÆÖµ¡£µ«ÊÇÔÚ»ùÓڼĴæÆ÷µÄϵͳÖУ¬Õâ¸ö 16 ½øÖƵÄÖµ»á´ÓÒ»¸öÕûÊý¼Ä´æÆ÷ÖжÁÈ¡£¬¶ø²»ÊÇ´Ó¸¡µã¼Ä´æÆ÷ÖжÁÈ¡¡£
½â¾ö·½°¸Êǽ«¸¡µã±äÁ¿µÄµØÖ·Ç¿ÖÆת»»³ÉÒ»¸öÖ¸ÏòÕûÐÍÀàÐ͵ÄÖ¸Õ룬ÈçÏÂËùʾ£º
printf ("The hex value of %f is %x", f, *(int *)&f);
½áÊøÓï
Ö÷Á÷µÄÓ²¼þ¹©Ó¦ÉÌ×î½ü¶¼ÔÚÀ©³ä×Ô¼ºµÄ 64 λ²úÆ·£¬ÕâÊÇÒòΪ 64 λƽ̨¿ÉÒÔÌṩ¸üºÃµÄÐÔÄÜ¡¢¼ÛÖµºÍ¿ÉÉìËõÐÔ¡£32 λϵͳµÄÏÞÖÆ£¬ÌرðÊÇ 4GB µÄÐéÄâÄÚ´æÉÏÏÞ£¬ÒѾ¼«´óµØ´Ì¼¤ºÜ¶à¹«Ë¾¿ªÊ¼¿¼ÂÇǨÒƵ½ 64 λƽ̨ÉÏ¡£Á˽âÈçºÎ½«Ó¦ÓóÌÐòÒÆÖ²µ½ 64 λÌåϵ½á¹¹ÉÏ¿ÉÒÔ°ïÖúÎÒÃDZàд¿ÉÒÆÖ²ÐÔ¸üºÃÇÒЧÂʸü¸ßµÄ´úÂë¡£
¹ØÓÚ×÷Õß
Harsha Adiga ¾ÍÖ°ÓÚÓ¡¶ÈµÄ IBM Software Group£¬Ëû²ÎÓëÁ˺ܶà Linux ºÍ¿ª·ÅÔ´ÂëÉçÇø¡¢¹¤×÷×éµÄ¹¤×÷¡£