# 二进制的原码反码补码

`原码`

早期用来表示数字的一种方式: 一个正数，转换为二进制位就是这个正数的原码。负数的绝对值转换成二进制位然后在高位补1就是这个负数的原码

举例说明：

int类型的 3 的原码是 11B(B表示二进制位)， 在32位机器上占四个字节，那么高位补零就得：

00000000 00000000 00000000 00000011

int类型的 -3 的绝对值的二进制位就是上面的 11B 展开后高位补零就得：

10000000 00000000 00000000 00000011

`反码`是什么 ？

正数的反码就是原码，负数的反码等于原码除符号位以外所有的位取反

举例说明：

int类型的 3 的反码是

00000000 00000000 00000000 00000011

和原码一样没什么可说的

int类型的 -3 的反码是

11111111 11111111 11111111 11111100

除开符号位 所有位 取反

解决了加减运算的问题，但还是有正负零之分，然后就到补码了

补码是什么？

正数的补码与原码相同，`负数的补码为 其原码除符号位外所有位取反（得到反码了），然后最低位加1`.

还是举例说明：

int类型的 3 的补码是：

00000000 00000000 00000000 00000011

int类型的 -3 的补码是

11111111 11111111 1111111 11111101

就是其反码加1

125的原码为0111 1101 -125的原码为1111 1101，反码为1000 0010，则补码为1000 0011

![](/files/-LokoVG01zlrbUsUPWOW)


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/interview/bu-ma.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.
