# 进制

常见的是进制如下表

| 进制   | 英文          | 前缀        | 数字                       | 例子           |
| ---- | ----------- | --------- | ------------------------ | ------------ |
| 二进制  | Binary      | 0b        | 共2个：0和1                  | 0b1110       |
| 八进制  | Octal       | 0o        | 共8个：0\~7                 | 0o1345       |
| 十进制  | Decimal     | 0d（或省略不写） | 共10个：0\~9                | 0d2435或者2435 |
| 十六进制 | Hexadecimal | 0x        | 共16个：0\~9和A\~F（或小写的a\~f） | 0xFF32       |

## 原码/反码/补码

在Java中，short类型数字占2个字节，2×8=16个位。对于short类型的数字3来说：其二进制表示为11，因为占用2个字节，前面的要用0补上，即0000 0000 0000 0011，如果是-3，则用1000 0000 0000 0011表示：最高位为0表示正数，最高位为1表示负数。

**原码**

将一个数值转换为二进制，就是这个数值的原码。

```
3  原码为0000 0000 0000 0011
-3 原码为1000 0000 0000 0011
```

**反码**

* 正数的反码就是自身的原码。
* 负数的反码是在自身原码的基础上，符号位不变 其它位都取反。

```
3  反码为0000 0000 0000 0011
-3 反码为1111 1111 1111 1100
```

**补码**

* 正数的补码就是自身的原码
* 负数的补码是在其反码的基础上加1

```
3  补码为0000 0000 0000 0011
-3 补码为1111 1111 1111 1101
```

表格列出如下:

| 类型 | 定义                                    | 举例：3                | 举例：-3               |
| -- | ------------------------------------- | ------------------- | ------------------- |
| 原码 | 数值的二进制                                | 0000 0000 0000 0011 | 1000 0000 0000 0011 |
| 反码 | 正数的反码就是其原码；负数的反码是在原码的基础上，符号位不变，其他位都取反 | 0000 0000 0000 0011 | 1111 1111 1111 1100 |
| 补码 | 正数的补码就是其原码；负数的补码是在反码的基础上加1            | 0000 0000 0000 0011 | 1111 1111 1111 1101 |

注：例子中的3为`short`类型

**TODO：为什么要用补码？**

## Java数据类型

| 类型 | 字节数     | (二进位制)位数 | 最小 | 最大                                | 默认                                  | 其他    |
| -- | ------- | -------- | -- | --------------------------------- | ----------------------------------- | ----- |
|    | byte    | 1        | 8  | -128(-27)                         | 127(27-1)                           | 0     |
|    | short   | 2        | 16 | -32768(-215)                      | 32767(215-1)                        | 0     |
|    | int     | 4        | 32 | - 2,147,483,648(-231)             | 2,147,483,647(231 -1)               | 0     |
|    | long    | 8        | 64 | -9,223,372,036,854,775,808(-2^63) | 9,223,372,036,854,775,807 (2^63 -1) | 0L    |
|    | float   | 4        | 32 |                                   |                                     | 0.0f  |
|    | double  | 8        | 64 |                                   |                                     | 0.0d  |
|    | boolean |          | 1  |                                   |                                     | false |
|    | char    | 2        | 16 |                                   |                                     |       |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maxwell.gitbook.io/way-to-architect/ji-suan-ji-cao-zuo-xi-tong/ji-suan-ji-ji-chu/jin-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
