> For the complete documentation index, see [llms.txt](https://craxclive.gitbook.io/crystal-lang-docs-zh_cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://craxclive.gitbook.io/crystal-lang-docs-zh_cn/syntax_and_semantics/literals/integers.md).

# 整数 Integers

有四种有符号整数类型和四种无符号整数类型：

| Type                                              | Length |  Minimum Value | Maximum Value |
| ------------------------------------------------- | -----: | -------------: | ------------: |
| [Int8](http://crystal-lang.org/api/Int8.html)     |      8 |           -128 |           127 |
| [Int16](http://crystal-lang.org/api/Int16.html)   |     16 |        −32,768 |        32,767 |
| [Int32](http://crystal-lang.org/api/Int32.html)   |     32 | −2,147,483,648 | 2,147,483,647 |
| [Int64](http://crystal-lang.org/api/Int64.html)   |     64 |           −263 |       263 - 1 |
| [UInt8](http://crystal-lang.org/api/UInt8.html)   |      8 |              0 |           255 |
| [UInt16](http://crystal-lang.org/api/UInt16.html) |     16 |              0 |        65,535 |
| [UInt32](http://crystal-lang.org/api/UInt32.html) |     32 |              0 | 4,294,967,295 |
| [UInt64](http://crystal-lang.org/api/UInt64.html) |     64 |              0 |       264 - 1 |

整数根据正负号 （`+` 或 `-` ，正号可省略）、`数字及下划线`的规则构成，也可以加上后缀。

如果不存在后缀，则字面量的类型是`Int32`, `Int64`和`UInt64`之间取最低者。如下例子所示：

```
1      # Int32

1_i8   # Int8
1_i16  # Int16
1_i32  # Int32
1_i64  # Int64

1_u8   # UInt8
1_u16  # UInt16
1_u32  # UInt32
1_u64  # UInt64

+10    # Int32
-20    # Int32

2147483648          # Int64
9223372036854775808 # UInt64
```

后缀前的 `_` 是可选的。

下划线也可以用于数字中间让其更具可读性：

```
1_000_000 # 可读性优于 1000000
```

二进制数字以 `0b`开头:

```
0b1101 # == 13
```

八进制数字以 `0o`开头:

```
0o123 # == 83
```

十六进制数字以`0x`开头:

```
0xFE012D # == 16646445
0xfe012d # == 16646445
```
