什么是“2的补码"? [英] What is “2's Complement”?

查看:21
本文介绍了什么是“2的补码"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习计算机系统课程,并且一直在挣扎,部分原因是 二的补码.我想理解它,但我读过的所有内容并没有为我带来图片.我已阅读 维基百科文章 和其他各种文章,包括 我的教科书.

I'm in a computer systems course and have been struggling, in part, with Two's Complement. I want to understand it but everything I've read hasn't brought the picture together for me. I've read the wikipedia article and various other articles, including my text book.

因此,我想开始这篇社区 wiki 帖子,以定义什么是二进制补码、如何使用它以及它如何在强制转换等操作期间影响数字(从有符号到无符号,反之亦然), 按位运算和位移运算.

Hence, I wanted to start this community wiki post to define what Two's Complement is, how to use it and how it can affect numbers during operations like casts (from signed to unsigned and vice versa), bit-wise operations and bit-shift operations.

我希望的是一个清晰简洁的定义,程序员很容易理解.

What I'm hoping for is a clear and concise definition that is easily understood by a programmer.

推荐答案

二的补码是一种存储整数的聪明方法,因此常见的数学问题很容易实现.

Two's complement is a clever way of storing integers so that common math problems are very simple to implement.

要理解,您必须考虑 binary 中的数字.

To understand, you have to think of the numbers in binary.

它基本上是说,

  • 对于零,使用全 0.
  • 对于正整数,开始向上计数,最多为 2(位数 - 1)-1.
  • 对于负整数,执行完全相同的操作,但转换 0 和 1 的角色并倒计时(因此,不是从 0000 开始,而是从 1111 开始 - 这是补码"部分).

让我们用 4 位的 mini-byte 尝试一下(我们称之为 nibble - 1/2 个字节).

Let's try it with a mini-byte of 4 bits (we'll call it a nibble - 1/2 a byte).

  • 0000 - 零
  • 0001 - 一个
  • 0010 - 两个
  • 0011 - 三
  • 01000111 - 四到七
  • 0000 - zero
  • 0001 - one
  • 0010 - two
  • 0011 - three
  • 0100 to 0111 - four to seven

这就是我们所能做的积极的事情.23-1 = 7.

That's as far as we can go in positives. 23-1 = 7.

对于否定:

  • 1111 - 负一
  • 1110 - 负二
  • 1101 - 负三
  • 11001000 - 负四到负八
  • 1111 - negative one
  • 1110 - negative two
  • 1101 - negative three
  • 1100 to 1000 - negative four to negative eight

请注意,您可以获得一个额外的负值 (1000 = -8),而正值则没有.这是因为 0000 用于零.这可以被视为计算机的数字线.

Note that you get one extra value for negatives (1000 = -8) that you don't for positives. This is because 0000 is used for zero. This can be considered as Number Line of computers.

区分正数和负数

这样做,第一位得到符号"的作用.位,因为它可用于区分非负十进制值和负十进制值.如果最高位是1,那么二进制可以说是负数,如果最高位(最左边)是0,你可以说十进制值是非负数.

Doing this, the first bit gets the role of the "sign" bit, as it can be used to distinguish between nonnegative and negative decimal values. If the most significant bit is 1, then the binary can be said to be negative, where as if the most significant bit (the leftmost) is 0, you can say the decimal value is nonnegative.

"Sign-magnitude" 负数只是翻转了它们的符号位正对应,但这种方法必须处理将 1000(一个 1 后跟所有 0s)解释为负零"的问题.这令人困惑.

"Sign-magnitude" negative numbers just have the sign bit flipped of their positive counterparts, but this approach has to deal with interpreting 1000 (one 1 followed by all 0s) as "negative zero" which is confusing.

一个人的补码" 负数只是它们的位补码正对应物,这也导致令人困惑的负零";与 1111 (所有).

"Ones' complement" negative numbers are just the bit-complement of their positive counterparts, which also leads to a confusing "negative zero" with 1111 (all ones).

除非您的工作非常接近硬件,否则您可能不必处理 Ones' Complement 或 Sign-Magnitude 整数表示.

You will likely not have to deal with Ones' Complement or Sign-Magnitude integer representations unless you are working very close to the hardware.

这篇关于什么是“2的补码"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆