> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/computer-basics/number-systems.md).

# 进制转换

### 常见进制

* 二进制：逢二进一，数字 0、1，前缀 `0b`
* 八进制：逢八进一，数字 0\~7，前缀 `0`
* 十进制：逢十进一，数字 0\~9
* 十六进制：逢十六进一，数字 0~~9、A~~F，前缀 `0x`

### 二进制转十进制

按权展开求和。例如 `1101₂ = 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 13`。

### 十进制转其他进制

**除基取余，逆序排列**。例如 13 转二进制：

```
13 ÷ 2 = 6 余 1
 6 ÷ 2 = 3 余 0
 3 ÷ 2 = 1 余 1
 1 ÷ 2 = 0 余 1
从下往上：1101
```

### 二进制与八/十六进制的快速互转

* **八进制**：每 3 位二进制对应 1 位八进制
* **十六进制**：每 4 位二进制对应 1 位十六进制

例如 `1101 0110₂ = D6₁₆`。

### C++ 中的进制输出

```cpp
#include <iostream>
using namespace std;

int main() {
    int n = 13;
    cout << n << endl;         // 13（十进制）
    cout << hex << n << endl;   // d（十六进制）
    cout << oct << n << endl;   // 15（八进制）
    cout << bitset<8>(n) << endl; // 00001101（二进制，需 <bitset>）
    return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/computer-basics/number-systems.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
