int

描述

整數型別是儲存一個數字的主要資料型別。

在 86Duino 上一個 int 能儲存 32-bit (4-byte) 的數值,範圍在 -2,147,483,648 到 2,147,483,647 之間 (最小值是 -(2^31) 而最大值是 (2^31)-1)。

int 儲存負整數是以二的補數計算方法來運算,最高位元稱為 ”sign”,用來表示是負數與否,1 即是負整數,0 則是正整數。

在 86Duino 上,你可以放心的在運算式中使用負數;但請注意:在處理 位元右移運算 時,有可能會出現不可預期的副作用。

範例


int ledPin = 13;

語法


int var = val;

var – 整數變數名稱
val – 指派數值給變數

提醒

當變數超過他們可表示的最大範圍時,會返回最小後再加上去,反之亦是如此。例如一個 32-bit 的整數:

   int x;
   x = -2147483648;
   x = x - 1;       // x 現在是 2,147,483,647 (因為範圍過頭而返回到正數)

   x = 2147483647;
   x = x + 1;       // x 現在是 -2,147,483,648 (因為範圍過頭而返回到負數)

See also

byte
unsigned int
long
unsigned long
Integer Constants
Variable Declaration


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.