如何计算用C计算机字长? [英] How to compute the word size of your computer in C?
问题描述
可能重复:结果
确定我的处理器字长
这是一个今天的面试问题。但我不知道...
It is One Interview question today. But I didn't know ...
我觉得面试官meaned CPU的字长。
I think the interviewer meaned the word size of cpu.
我觉得像这样的答案:
int cpu_bits(void *dummy1, void *dummy2)
{
long offset = (long)&dummy2 - (long)&dummy1;
int ret = 0;
if (8 == offset)
ret = 64;
else if (4 == offset)
ret = 32;
else if (2 == offset)
ret = 16;
else if (1 == offset)
ret = 8;
else
ret = -1;
return ret;
}
int main()
{
printf("%d\n", cpu_bits(NULL, NULL));
return 0;
}
的结果似乎是正确的,你这样认为吗?
The result seems to be right, Do you think so ?
推荐答案
简短的回答:标准没有定义是保证对应于底层架构的字大小的数据类型,以及什么叫字的大小是指现代的CPU是一个相当模糊的事情:字大小与地址大小
Short answer: the standard does not define a data type that is guaranteed to correspond to the word size of the underlying architecture, and what "word size" means on modern CPU's is quite a vague thing: Word Size versus Address Size.
对于具有兼容模式,不同大小的寄存器当前处理器,先进的解决适用于各种宽度的数据模式和指令,一般的字大小的交谈是即时通讯precise,至少可以这样说。
With current processors having compatibility modes, registers of a different size, advanced addressing modes and instructions suited for data of various widths, talking of a general "word size" is imprecise, to say the least.
我想面试官仍住在上世纪90年代,并记得是在半信半疑名为字
和 DWORD
类型通过WinAPI的介绍时,大多数电脑仍然为16位。
I suppose the interviewer is still living in the 90's and remembers the dubiously called WORD
and DWORD
types that were introduced by WinAPI when most computers were still 16-bit.
这篇关于如何计算用C计算机字长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!