What is the size of integer in 8-bit, 16-bit, 32-bit processors/microcontrollers?

我与影子孤独终老i 提交于 2019-12-07 12:43:38

问题


What is the size of integer in 8-bit, 16-bit, 32-bit processors/microcontrollers ? I guess it depends on the internal accumulator/register size. But not sure. Thanks


回答1:


I'm only aware of one programming language that defines an integer data type, but it's seldom used for 8 and 16-bit architectures. C is the most widely used language for programming 8-bit, 16-bit, and 32-bit architectures, so I assume you are looking for an answer in the context of C.

There are several "integer" data types in C: char, short, int, long, etc..., but I will assume what you really mean is the int data type.

The size of an int is not defined by the architecture, it is defined by the the C programming language specification, and it's extremely vague.

A ‘‘plain’’ int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

I interpret this to mean it is determined by the implementation of the compiler.

You can find the latest publicly available version of the C11 standard (at the time of writing this answer) here: http://www.open-std.org/jtc1/sc22/wg14/www/standards.html.

Here are some tests I ran to help answer this question:

  • On an 8-bit Atmel AVR Arduino, sizeof(int) returns 2 (e.g. 16-bits) when compiled with GCC 4.3.2 (WinAVR 20081205)
  • Don't have a 16-bit MCU or compiler, sorry!
  • On a 32-bit ARM Cortex-M MCU, sizeof(int) returns 4 (e.g. 32-bits) when compiled with GCC 4.9.2.
  • On a 64-bit Intel Core i7 CPU, sizeof(int) returns 4 (e.g. 32-bits) regardless of whether it is compiled for 32-bit or 64-bit. Tested with both Visual Studio 2013 and GCC 4.9.2.

A more interesting answer would be why those values were chosen.




回答2:


C:

Integer is defined by the c-standard as the "natural" data size. Implementers of C-Compilers for various platforms are allowed to set the integer size mostly at will. I think the idea behind all that is that int should reflect the data size which works best (fastest, ressource friendliest) for the given platform.

  • Microchip XC8: 16 bit
  • Microchip XC16: 16 bit
  • Microchip XC32: 32 bit
  • Keil: 32 bit



回答3:


Integers can be of any size. The processor doesn't have much say in it. For example, in Java, int = 32 bits. In VB.NET, there exists Int16, Int32, and Int64, while the default Integer = 32 bits.



来源:https://stackoverflow.com/questions/29618439/what-is-the-size-of-integer-in-8-bit-16-bit-32-bit-processors-microcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!