Int

来自YFRobotwiki
跳转至: 导航搜索

描述

Integers are your primary data-type for number storage.

On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes referred to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added.

The Arduino takes care of dealing with negative numbers for you, so that arithmetic operations work transparently in the expected manner. There can be an unexpected complication in dealing with the bitshift right operator (>>) however.

译:

整数是基本数据类型。

在UNO(和其他ATMega控制板)上,占用16bit即2字节。整数的范围为-32,768到32,767( -2^15 ~(2^15)-1)。

在DUE和SAMD(像:MKR1000和Zero)上,占用32bit即4字节。整数的范围为-2,147,483,648到2,147,483,647( -2^31 ~(2^31)-1)。

整数类型使用2的补码方式存储负数。最高位通常为符号位,表示数的正负。其余位被“取反加1”。

Arduino为您处理负数计算问题,所以数学计算对您是透明的。但是,当处理右移位运算符(»)时,可能有未预期的编译过程。


示例

    int ledPin = 13;


语法

    int var = val;

- var - your int variable name

- val - the value you assign to that variable


编程提示

When variables are made to exceed their maximum capacity they "roll over" back to their minimum capacity, note that this happens in both directions.

译: 当变量数值过大而超过整数类型所能表示的范围时(-32,768到32,767),变量值会“回滚”

Example for a 16-bit int:

   int x;
   x = -32768;
   x = x - 1;       // x now contains 32,767 - rolls over in neg. direction

   x = 32767;
   x = x + 1;       // x now contains -32,768 - rolls over


扩展阅读

- byte
- unsigned int
- long
- unsigned long


返回Arduino语法参考列表

更多建议和问题欢迎反馈至 YFRobot论坛