整數(shù) (Integers)
Move supports three unsigned integer types: u8, u64, and u128. Values of these types range from 0 to a maximum that depends on the size of the type.
| Type | Value Range |
|---|---|
Unsigned 8-bit integer, u8
|
0 to 28 - 1 |
Unsigned 64-bit integer, u64
|
0 to 264 - 1 |
Unsigned 128-bit integer, u128
|
0 to 2128 - 1 |
Move 支持三種無(wú)符號(hào)整數(shù)類型:u8、u64 和 u128。這些類型的值范圍從 0 到最大值,最大值的具體取值取決于整數(shù)類型。
| 類型 | 取值范圍 |
|---|---|
無(wú)符號(hào) 8位 整數(shù), u8
|
0 to 28 - 1 |
無(wú)符號(hào) 64位 整數(shù), u64
|
0 to 264 - 1 |
無(wú)符號(hào) 128位 整數(shù), u128
|
0 to 2128 - 1 |
字面值(Literal)
Literal values for these types are specified either as a sequence of digits (e.g.,112) or as hex literals, e.g., 0xFF. The type of the literal can optionally be added as a suffix, e.g., 112u8. If the type is not specified, the compiler will try to infer the type from the context where the literal is used. If the type cannot be inferred, it is assumed to be u64.
If a literal is too large for its specified (or inferred) size range, an error is reported.
(在Move中)這些類型的字面值指定為數(shù)字序列(例如:112)或十六進(jìn)制文字(例如:0xFF), 可以選擇將字面值的類型定義為后綴, 例如 112u8。如果未指定類型,編譯器將嘗試從使用字面值的上下文推斷類型。如果無(wú)法推斷類型,則默認(rèn)為 `u64。
如果字面值太大,超出其指定的(或推斷的)大小范圍,則會(huì)報(bào)錯(cuò)。
例如:
// literals with explicit annotations;
let explicit_u8 = 1u8;
let explicit_u64 = 2u64;
let explicit_u128 = 3u128;
// literals with simple inference
let simple_u8: u8 = 1;
let simple_u64: u64 = 2;
let simple_u128: u128 = 3;
// literals with more complex inference
let complex_u8 = 1; // inferred: u8
// right hand argument to shift must be u8
let _unused = 10 << complex_u8;
let x: u8 = 0;
let complex_u8 = 2; // inferred: u8
// arguments to `+` must have the same type
let _unused = x + complex_u8;
let complex_u128 = 3; // inferred: u128
// inferred from function argument type
function_that_takes_u128(complex_u128);
// literals can be written in hex
let hex_u8: u8 = 0x1;
let hex_u64: u64 = 0xCAFE;
let hex_u128: u128 = 0xDEADBEEF;
運(yùn)算集 (Operations)
算術(shù)運(yùn)算 (Arithmetic)
Each of these types supports the same set of checked arithmetic operations. For all of these operations, both arguments (the left and right side operands) must be of the same type. If you need to operate over values of different types, you will need to first perform a cast. Similarly, if you expect the result of the operation to be too large for the integer type, perform a cast to a larger size before performing the operation.
每一種(無(wú)符號(hào)整數(shù))類型都支持相同算術(shù)運(yùn)算集。對(duì)于所有這些運(yùn)算,兩個(gè)參數(shù)(左側(cè)和右側(cè)操作數(shù))必須是同一類型。如果您需要對(duì)不同類型的值進(jìn)行運(yùn)算,則需要首先執(zhí)行強(qiáng)制轉(zhuǎn)換。同樣,如果您預(yù)計(jì)運(yùn)算結(jié)果對(duì)于當(dāng)下整數(shù)類型來(lái)說(shuō)太大,請(qǐng)?jiān)趫?zhí)行運(yùn)算之前將之轉(zhuǎn)換為更大的整數(shù)類型。
All arithmetic operations abort instead of behaving in a way that mathematical integers would not (e.g., overflow, underflow, divide-by-zero).
| Syntax | Operation | Aborts If |
|---|---|---|
+ |
addition | Result is too large for the integer type |
- |
subtraction | Result is less than zero |
* |
multiplication | Result is too large for the integer type |
% |
modular division | The divisor is 0
|
/ |
truncating division | The divisor is 0
|
Bitwise
算術(shù)運(yùn)算在遇到異常時(shí)將會(huì)中止,而不是以上溢、下溢、被零除等數(shù)學(xué)整數(shù)未定義的的方式輸出結(jié)果。
| 句法 | 操作 | 中止條件 |
|---|---|---|
+ |
加法 | 結(jié)果對(duì)于整數(shù)類型來(lái)說(shuō)太大了 |
- |
減法 | 結(jié)果小于零 |
* |
乘法 | 結(jié)果對(duì)于整數(shù)類型來(lái)說(shuō)太大了 |
% |
取余運(yùn)算 | 除數(shù)為 0
|
/ |
截?cái)喑?/td> | 除數(shù)為 0
|
位運(yùn)算 (Bitwise)
The integer types support the following bitwise operations that treat each number as a series of individual bits, either 0 or 1, instead of as numerical integer values.
Bitwise operations do not abort.
| Syntax | Operation | Description | |
|---|---|---|---|
& |
bitwise and | Performs a boolean and for each bit pairwise | |
| ` | ` | bitwise or | Performs a boolean or for each bit pairwise |
^ |
bitwise xor | Performs a boolean exclusive or for each bit pairwise |
整數(shù)類型支持下列位運(yùn)算,即將每個(gè)數(shù)字視為一系列單獨(dú)的位:0 或 1,而不是整型數(shù)值。
位運(yùn)算不會(huì)中止。
| 句法 | 操作 | 描述 | |
|---|---|---|---|
& |
按位 和 | 對(duì)每個(gè)位成對(duì)執(zhí)行布爾值和 | |
| ` | ` | 按位或 | 對(duì)每個(gè)位成對(duì)執(zhí)行布爾值或 |
^ |
按位 異與 | 對(duì)每個(gè)位成對(duì)執(zhí)行布爾異或 |
位移 (Bit shift)
Similar to the bitwise operations, each integer type supports bit shifts. But unlike the other operations, the righthand side operand (how many bits to shift by) must always be a u8 and need not match the left side operand (the number you are shifting).
Bit shifts can abort if the number of bits to shift by is greater than or equal to 8, 64, or 128 for u8, u64, and u128 respectively.
| Syntax | Operation | Aborts if |
|---|---|---|
<< |
shift left | Number of bits to shift by is greater than the size of the integer type |
>> |
shift right | Number of bits to shift by is greater than the size of the integer type |
與按位運(yùn)算類似,每種整數(shù)類型都支持位移(bit shifts)。但與其他運(yùn)算不同的是,右側(cè)操作數(shù)(要移位多少位)必須始終是 u8 并且不需要與左側(cè)操作數(shù)類型(您要移位的數(shù)字)匹配。
如果要移位的位數(shù)分別大于或等于 8、64, u128 或 128 的 u8, u64, 則移位可以中止。
| 句法 | 操作 | 中止條件 |
|---|---|---|
<< |
左移 | 要移位的位數(shù)大于整數(shù)類型的大小 |
>> |
右移 | 要移位的位數(shù)大于整數(shù)類型的大小 |
比較運(yùn)算 (Comparisons)
Integer types are the only types in Move that can use the comparison operators. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to cast one of them first.
Comparison operations do not abort.
| Syntax | Operation |
|---|---|
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
整數(shù)類型是 Move 中唯一可以使用比較(Comparisons)運(yùn)算符的類型。兩個(gè)參數(shù)必須是同一類型。如果您需要比較不同類型的整數(shù),則需要先轉(zhuǎn)換其中一個(gè)。
比較操作不會(huì)中止。
| 句法 | 操作 |
|---|---|
< |
小于 |
> |
大于 |
<= |
小于等于 |
>= |
大于等于 |
相等 (Equality)
Like all types with drop in Move, all integer types support the "equal" and "not equal" operations. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to cast one of them first.
Equality operations do not abort.
| Syntax | Operation |
|---|---|
== |
equal |
!= |
not equal |
For more details see the section on equality
與 Move 中的所有具有drop能力的類型一樣,所有整數(shù)類型都支持 "equal(等于)" 和 "not equal(不等于)運(yùn)算。兩個(gè)參數(shù)必須是同一類型。如果您需要比較不同類型的整數(shù),則需要先轉(zhuǎn)換其中一個(gè)。
相等(Equality)運(yùn)算不會(huì)中止。
| 句法 | 操作 |
|---|---|
== |
等于 |
!= |
不等于 |
更多細(xì)節(jié)可以參考相等章節(jié)。
轉(zhuǎn)換 (Casting)
Integer types of one size can be cast to integer types of another size. Integers are the only types in Move that support casting.
Casts do not truncate. Casting will abort if the result is too large for the specified type
| Syntax | Operation | Aborts if |
|---|---|---|
(e as T) |
Cast integer expression e into an integer type T
|
e is too large to represent as a T
|
Here, the type of e must be u8, u64, or u128 and T must be u8, u64, or u128.
For example:
(x as u8)(2u8 as u64)(1 + 3 as u128)
一種大小的整數(shù)類型可以轉(zhuǎn)換為另一種大小的整數(shù)類型。整數(shù)是 Move 中唯一支持強(qiáng)制轉(zhuǎn)換的類型。
強(qiáng)制轉(zhuǎn)換不會(huì)截?cái)?。如果結(jié)果對(duì)于指定類型來(lái)說(shuō)太大,則轉(zhuǎn)換將中止。
| Syntax | 操作 | 中止條件 |
|---|---|---|
(e as T) |
將整數(shù)表達(dá)式 e 轉(zhuǎn)換為整數(shù)類型 T
|
e 太大而不能表示為 T
|
所有權(quán) (Ownership)
As with the other scalar values built-in to the language, integer values are implicitly copyable, meaning they can be copied without an explicit instruction such as copy.
與語(yǔ)言內(nèi)置的其他標(biāo)量值一樣,整數(shù)值是隱式可復(fù)制的,這意味著它們可以在沒(méi)有明確指令如copy的情況下復(fù)制。