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論壇