对于有符号数,为什么更喜欢二进制补码而不是符号和数值?
我只是好奇是否有理由为了用二进制表示-1,使用二进制补码:翻转位并加1?
-1 由 11111111 (二进制补码)表示,而不是(对我来说更直观) 10000001 表示,后者是二进制 1,第一位作为负标志。
免责声明:我的工作不依赖二进制算术!
I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1?
-1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1 with first bit as negative flag.
Disclaimer: I don't rely on binary arithmetic for my job!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
这样做是为了使加法不需要任何特殊的逻辑来处理负数。 查看维基百科上的文章。
假设您有两个数字:2 和 -1。 按照您表示数字的“直观”方式,它们将分别为
0010
和1001
(我坚持使用 4 位大小)。 在二进制补码方式中,它们是0010
和1111
。 现在,假设我想添加它们。二进制补码加法非常简单。 您可以正常添加数字,并且末尾的任何进位位都会被丢弃。 因此它们的添加如下:
0001
为 1,这是“2+(-1)”的预期结果。但在你的“直观”方法中,添加更复杂:
哪个是-3,对吧? 在这种情况下,简单的加法不起作用。 您需要注意其中一个数字是负数,如果是这种情况,请使用不同的算法。
对于这种“直观”的存储方法,减法是与加法不同的运算,需要在数字相加之前对数字进行额外的检查。 由于您希望最基本的运算(加法、减法等)尽可能快,因此您需要以一种允许您使用尽可能简单的算法的方式存储数字。
另外,在“直观”存储方法中,有两个零:
直观上它们是相同的数字,但在存储时具有两个不同的值。 每个应用程序都需要采取额外的步骤来确保非零值也不是负零。
以这种方式存储整数还有另一个好处,那就是当您需要扩展存储值的寄存器的宽度时。使用二进制补码,在 8 位寄存器中存储 4 位数字只需重复其最高有效位:
只需查看较小单词的符号位并重复它,直到它填充较大单词的宽度。
使用您的方法,您需要清除现有位,这是除了填充之外的额外操作:
在两种情况下您仍然需要设置这些额外的 4 位,但在“直观”情况下,您需要清除第 5 位,如下所示出色地。 这是每个应用程序中最基本、最常见的操作之一中的一个微小的额外步骤。
It's done so that addition doesn't need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia.
Say you have two numbers, 2 and -1. In your "intuitive" way of representing numbers, they would be
0010
and1001
, respectively (I'm sticking to 4 bits for size). In the two's complement way, they are0010
and1111
. Now, let's say I want to add them.Two's complement addition is very simple. You add numbers normally and any carry bit at the end is discarded. So they're added as follows:
0001
is 1, which is the expected result of "2+(-1)".But in your "intuitive" method, adding is more complicated:
Which is -3, right? Simple addition doesn't work in this case. You need to note that one of the numbers is negative and use a different algorithm if that's the case.
For this "intuitive" storage method, subtraction is a different operation than addition, requiring additional checks on the numbers before they can be added. Since you want the most basic operations (addition, subtraction, etc) to be as fast as possible, you need to store numbers in a way that lets you use the simplest algorithms possible.
Additionally, in the "intuitive" storage method, there are two zeroes:
Which are intuitively the same number but have two different values when stored. Every application will need to take extra steps to make sure that non-zero values are also not negative zero.
There's another bonus with storing ints this way, and that's when you need to extend the width of the register the value is being stored in. With two's complement, storing a 4-bit number in an 8-bit register is a matter of repeating its most significant bit:
It's just a matter of looking at the sign bit of the smaller word and repeating it until it pads the width of the bigger word.
With your method you would need to clear the existing bit, which is an extra operation in addition to padding:
You still need to set those extra 4 bits in both cases, but in the "intuitive" case you need to clear the 5th bit as well. It's one tiny extra step in one of the most fundamental and common operations present in every application.
维基百科说明了一切:
换句话说,无论数字是否为负数,加法都是一样的。
Wikipedia says it all:
In other words, adding is the same, wether or not the number is negative.
尽管这个问题很老了,但让我投入 2 美分。
在解释这一点之前,让我们回到基础知识。 2' 补码是 1 的补码 + 1 。
现在什么是 1 的补码以及它的加法意义是什么。
任何 n 位数字及其 1 的补码之和给出了这些 n 位可以表示的最大可能数字。
示例:
现在如果我们尝试在结果中再加 1 会发生什么。 它将导致溢出。
结果将是
1 0000
,即 0(因为我们正在处理 4 位数字,(左边的 1 是溢出)所以,
有人决定将 1 的补码 + 1 称为 2' 补码所以上面的语句就变成了:
任何 n 位数字 + 其 2 的补码 = 0
这意味着一个数的 2 补码 = - (该数的)
所有这些又产生了一个问题,为什么我们只能使用 n 位中的 (n-1) 来表示正数以及为什么最左边的第 n 位表示符号(最左边的位0 表示+ve 数,1 表示-ve 数)。 例如,如果第 32 位是 1(它是一个数字),为什么我们只使用 java 中 int 的前 31 位来表示正数。
1 0000 (结果为零,进位 1 溢出)
因此 (n + 2' n 的补码) = 0 的系统仍然有效。 这里唯一不明确的是 12 的 2 补码是 0100 ,除了在 2 补码系统中表示 -12 之外,它还模糊地表示 +8 。
如果正数的最左边位总是有 0,这个问题就可以解决。 在这种情况下,它们的 2 的补码在最左边的位中总是有 1,并且表示 2 的补码数和 +ve 数的同一组位不会产生歧义。
Even though this question is old , let me put in my 2 cents.
Before I explain this ,lets get back to basics. 2' complement is 1's complement + 1 .
Now what is 1's complement and what is its significance in addition.
Sum of any n-bit number and its 1's complement gives you the highest possible number that can be represented by those n-bits.
Example:
Now what will happen if we try to add 1 more to the result. It will results in an overflow.
The result will be
1 0000
which is 0 ( as we are working with 4 bit numbers , (the 1 on left is an overflow )So ,
Someone then decided to call 1's complement + 1 as 2'complement. So the above statement becomes:
Any n'bit number + its 2's complement = 0
which means 2's complement of a number = - (of that number)
All this yields one more question , why can we use only the (n-1) of the n bits to represent positive number and why does the left most nth bit represent sign (0 on the leftmost bit means +ve number , and 1 means -ve number ) . eg why do we use only the first 31 bits of an int in java to represent positive number if the 32nd bit is 1 , its a -ve number.
1 0000 (result is zero , with the carry 1 overflowing)
Thus the system of (n + 2'complement of n) = 0 , still works. The only ambiguity here is 2's complement of 12 is 0100 which ambiguously also represents +8 , other than representing -12 in 2s complement system.
This problem will be solved if positive numbers always have a 0 in their left most bit. In that case their 2's complement will always have a 1 in their left most bit , and we wont have the ambiguity of the same set of bits representing a 2's complement number as well as a +ve number.
二进制补码允许以正常方式完成加法和减法(就像你为无符号数字)。 它还可以防止 -0(一种表示 0 的单独方法,但使用正常的逐位比较数字方法不会等于 0)。
Two's complement allows addition and subtraction to be done in the normal way (like you wound for unsigned numbers). It also prevents -0 (a separate way to represent 0 that would not be equal to 0 with the normal bit-by-bit method of comparing numbers).
二进制补码允许负数和正数相加,无需任何特殊逻辑。
如果您尝试使用您的方法添加 1 和 -1
10000001 (-1)
+00000001 (1)
你得到
10000010 (-2)
相反,通过使用二进制补码,我们可以添加
11111111 (-1)
+00000001 (1)
你得到
00000000 (0)
减法也是如此。
另外,如果你尝试从 6(两个正数)中减去 4,你可以用 2 的补码 4 并将两者相加 6 + (-4) = 6 - 4 = 2
这意味着正数和负数的减法和加法都可以所有这些都由CPU中的同一电路完成。
Two's complement allows negative and positive numbers to be added together without any special logic.
If you tried to add 1 and -1 using your method
10000001 (-1)
+00000001 (1)
you get
10000010 (-2)
Instead, by using two's complement, we can add
11111111 (-1)
+00000001 (1)
you get
00000000 (0)
The same is true for subtraction.
Also, if you try to subtract 4 from 6 (two positive numbers) you can 2's complement 4 and add the two together 6 + (-4) = 6 - 4 = 2
This means that subtraction and addition of both positive and negative numbers can all be done by the same circuit in the cpu.
这是为了简化数字的和与差。 以 2 的补码形式编码的负数和正数之和与以正常方式求和相同。
this is to simplify sums and differences of numbers. a sum of a negative number and a positive one codified in 2's complements is the same as summing them up in the normal way.
该操作的通常实现是“翻转位并加 1”,但还有另一种定义它的方法,这可能会使基本原理更加清晰。 2 的补码是采用通常的无符号表示形式时得到的形式,其中每个位控制 2 的下一个幂,并且仅将最高有效项设为负数。
取 8 位值 a7 a6 a5 a4 a3 > a2 a1 a0
通常的无符号二进制解释是:
27*a7 + 26*a6 + 25* a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*一个0
11111111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
二进制补码解释为:
-27*a7 + 26*a6 + 25 *a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20 *a0
11111111 = -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
其他位根本没有改变含义,并且进入 7 是“溢出”并且不是预期的工作,所以几乎所有算术运算都无需修改即可工作(正如其他人指出的那样)。 符号幅度通常检查符号位并使用不同的逻辑。
The usual implementation of the operation is "flip the bits and add 1", but there's another way of defining it that probably makes the rationale clearer. 2's complement is the form you get if you take the usual unsigned representation where each bit controls the next power of 2, and just make the most significant term negative.
Taking an 8-bit value a7 a6 a5 a4 a3 a2 a1 a0
The usual unsigned binary interpretation is:
27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0
11111111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
The two's complement interpretation is:
-27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0
11111111 = -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
None of the other bits change meaning at all, and carrying into a7 is "overflow" and not expected to work, so pretty much all of the arithmetic operations work without modification (as others have noted). Sign-magnitude generally inspect the sign bit and use different logic.
扩展其他答案:
在二进制补码中
分裂确实需要不同的机制。
所有这些都是正确的,因为二进制补码只是普通的模算术,我们选择通过减去模数来将某些数字视为负数。
To expand on others answers:
In two's complement
Division does require a different mechanism.
All these are true because two's complement is just normal modular arithmetic, where we choose to look at some numbers as negative by subtracting the modulo.
阅读这个问题的答案时,我发现了这个评论[编辑]。
在我看来,此评论中提出的问题非常有趣,因此我想首先重新措辞,然后提供答案和示例。
问题 – 系统如何确定如何解释一个或多个相邻字节? 特别是,系统如何确定给定的字节序列是普通二进制数还是 2 的补码数?
解答 – 系统确定如何通过类型解释字节序列。
类型定义
示例 - 下面我们假设
char
的长度为 1 个字节,short
的长度为 2 个字节int
和float
的长度为 4 个字节。请注意,这些大小特定于我的系统。 尽管很常见,但它们可能因系统而异。 如果您对它们在系统上的内容感到好奇,请使用 sizeof 运算符。
首先我们定义一个包含4个字节的数组,并将它们全部初始化为二进制数
10111101
,对应于十六进制数BD
。然后我们使用不同的类型读取数组内容。
无符号字符
和有符号字符
无符号短
和短
无符号整数
,int
和float
RAM 中的 4 个字节 (
l_Just4Bytes[ 0..3 ]
) 始终保持完全相同。 唯一改变的是我们如何解释它们。再次,我们告诉系统如何通过类型来解释它们。
例如,上面我们使用以下类型来解释
l_Just4Bytes
数组unsigned char
的内容:纯二进制signed char
中的 1 个字节: 2 的补码中的 1 个字节unsigned Short
:纯二进制表示法中的 2 个字节short
:2 的补码中的 2 个字节unsigned int
:纯二进制中的 4 个字节表示法int
:2 补码中的 4 个字节float
:IEEE 754 单精度表示法中的 4 个字节[编辑] 这篇文章已在 user4581301 发表评论后进行了编辑。 感谢您抽出宝贵的时间留下这几条有用的信息!
Reading the answers to this question, I came across this comment [edited].
In my opinion, the question asked in this comment is quite interesting and so I'd like first of all to rephrase it and then to provide an answer and an example.
QUESTION – How can the system establish how one or more adjacent bytes have to be interpreted? In particular, how can the system establish whether a given sequence of bytes is a plain binary number or a 2's complement number?
ANSWER – The system establishes how to interpret a sequence of bytes through types.
Types define
EXAMPLE – Below we assume that
char
's are 1 byte longshort
's are 2 bytes longint
's andfloat
's are 4 bytes longPlease note that these sizes are specific to my system. Although pretty common, they can be different from system to system. If you're curious of what they are on your system, use the sizeof operator.
First of all we define an array containing 4 bytes and initialize all of them to the binary number
10111101
, corresponding to the hexadecimal numberBD
.Then we read the array content using different types.
unsigned char
andsigned char
unsigned short
andshort
unsigned int
,int
andfloat
The 4 bytes in RAM (
l_Just4Bytes[ 0..3 ]
) always remain exactly the same. The only thing that changes is how we interpret them.Again, we tell the system how to interpret them through types.
For instance, above we have used the following types to interpret the contents of the
l_Just4Bytes
arrayunsigned char
: 1 byte in plain binarysigned char
: 1 byte in 2's complementunsigned short
: 2 bytes in plain binary notationshort
: 2 bytes in 2's complementunsigned int
: 4 bytes in plain binary notationint
: 4 bytes in 2's complementfloat
: 4 bytes in IEEE 754 single-precision notation[EDIT] This post has been edited after the comment by user4581301. Thank you for taking the time to drop those few helpful lines!
使用二进制补码是因为它在电路中实现起来更简单,并且不允许负零。
如果有 x 位,则二进制补码的范围为 +(2^x/2+1) 到 -(2^x/2)。 补码将从 +(2^x/2) 运行到 -(2^x/2),但允许负零(在 4 位 1 的补码系统中,0000 等于 1000)。
Two's complement is used because it is simpler to implement in circuitry and also does not allow a negative zero.
If there are x bits, two's complement will range from +(2^x/2+1) to -(2^x/2). One's complement will run from +(2^x/2) to -(2^x/2), but will permit a negative zero (0000 is equal to 1000 in a 4 bit 1's complement system).
表示数字的负数表示的常用方法有以下三种。
要了解为什么 2 的补码优于其他两种,让我们看看其他方法有哪些限制。
使用有符号位时的问题:
使用 1 的补码时出现的问题:
在 1 的补码中,负数是通过反转其正数表示的位来表示的。
问题与零有两种表示形式相同,即负零和正零。 这将缩小范围并在算术运算中造成混乱。
2的补码如何解决这个问题
注意:在计算 2 的补码时,末尾的任何进位都会被丢弃。
算术运算更容易执行,因为 2 的补码形式源自 0-x 的思想,其中 x 是数字(正数形式)。
负数的前导位始终为 1,正数的前导位始终为 0。
There are three common methods to represent negative representation of a number which are as below
To understand why 2's Complement is preferred over other two, let us see what limitations other methods posses.
Problem while using Signed bit:
Problem while using 1's Complement:
In 1's Complement, negative number is represented by reversing the bits of its positive representation.
Problem is same as there will two representations of zero i.e minus and plus zero. This will reduce the range as well as create confusion in arithmetic operations.
How 2's Complement solve the issue
Note: Any carry bit at the end is discarded while calculating 2's Complement.
Arithmetic operations are easier to perform, because 2’s complement form is derived from idea of 0-x where x is the number (positive form).
Leading bit is always 1 for the negative number and 0 for the positive number.
值得注意的是,在数字计算机出现之前的一些早期加法机上,减法是通过让操作员在每个键上使用不同颜色的图例组输入值来执行的(因此每个键将输入 9 减去要计算的数字)。减去),然后按一个特殊按钮将假设进位进入计算。 因此,在六位机器上,要从某个值中减去 1234,操作员需要按下通常表示“998,765”的按键,然后按下按钮将该值加一到正在进行的计算中。 二进制补码算术只是早期“十补码”算术的二进制等价物。
It's worthwhile to note that on some early adding machines, before the days of digital computers, subtraction would be performed by having the operator enter values using a different colored set of legends on each key (so each key would enter nine minus the number to be subtracted), and press a special button would would assume a carry into a calculation. Thus, on a six-digit machine, to subtract 1234 from a value, the operator would hit keys that would normally indicate "998,765" and hit a button to add that value plus one to the calculation in progress. Two's complement arithmetic is simply the binary equivalent of that earlier "ten's-complement" arithmetic.
通过补码法进行减法的优点是可以减少硬件
复杂性。不需要不同的数字电路进行加法和减法。两者
加法和减法仅由加法器执行。
The advantage of performing subtraction by the complement method is reduction in the hardware
complexity.The are no need of the different digital circuit for addition and subtraction.both
addition and subtraction are performed by adder only.
我有一个在某些情况下很重要的补充:在考虑到这些限制的情况下,二进制补码是唯一可能的表示形式:
要明白为什么,它有助于减少基数; 例如,Z_4。
符号和大小和补码都不会形成具有相同数量元素的环; 一个症状是双零。 因此,很难在边缘处进行操作; 为了在数学上保持一致,它们需要检查溢出或陷阱表示。
I have a slight addendum that is important in some situations: two's compliment is the only representation that is possible given these constraints:
To see why, it helps to reduce the cardinality; for example, Z_4.
Sign and magnitude and ones' compliment both do not form a ring with the same number of elements; a symptom is the double zero. It is therefore difficult to work with on the edges; to be mathematically consistent, they require checking for overflow or trap representations.
好吧,您的意图并不是真正反转二进制数的所有位。 它实际上是用 1 减去它的每一位数字。幸运的是,1 减 1 得到 0,1 减 0 得到 1。所以翻转位实际上是在执行这个减法。
但为什么你要找出每个数字与 1 的差异呢? 好吧,你不是。 您的实际目的是计算给定的二进制数与另一个具有相同位数但仅包含 1 的二进制数的差异。 例如,如果您的数字是 10110001,当您翻转所有这些位时,您实际上正在计算 (11111111 - 10110001)。
这解释了计算补码的第一步。 现在让我们将第二步——添加 1——也包含在图中。
将上面的二元方程加 1:
11111111 - 10110001 + 1
你会得到什么? 这个:
100000000 - 10110001
这是最终的方程。 通过执行这两个步骤,您试图找到最终的区别:从另一个二进制数中减去一个额外的数字并包含除最高有效位位置之外的零的二进制数。
但为什么我们真的渴望这种差异呢? 好吧,从现在开始,我想如果您阅读维基百科文章会更好。
Well, your intent is not really to reverse all bits of your binary number. It is actually to subtract each its digit from 1. It's just a fortunate coincidence that subtracting 1 from 1 results in 0 and subtracting 0 from 1 results in 1. So flipping bits is effectively carrying out this subtraction.
But why are you finding each digit's difference from 1? Well, you're not. Your actual intent is to compute the given binary number's difference from another binary number which has the same number of digits but contains only 1's. For example if your number is 10110001, when you flip all those bits, you're effectively computing (11111111 - 10110001).
This explains the first step in the computation of Two's Complement. Now let's include the second step -- adding 1 -- also in the picture.
Add 1 to the above binary equation:
11111111 - 10110001 + 1
What do you get? This:
100000000 - 10110001
This is the final equation. And by carrying out those two steps you're trying to find this, final difference: the binary number subtracted from another binary number with one extra digit and containing zeros except at the most signification bit position.
But why are we hankerin' after this difference really? Well, from here on, I guess it would be better if you read the Wikipedia article.
加法和减法都只执行加法运算。 我们将第二个操作数添加到第一个操作数上进行加法。 对于减法,我们将第二个操作数的 2 补码与第一个操作数相加。
通过 2 的补码表示,我们不需要单独的数字组件来进行减法,只使用加法器和补码器。
We perform only addition operation for both addition and subtraction. We add the second operand to the first operand for addition. For subtraction we add the 2's complement of the second operand to the first operand.
With a 2's complement representation we do not need separate digital components for subtraction—only adders and complementers are used.
为什么使用 Two2 的补码而不是 One 的补码系统来表示负数,一个令人满意的答案是:
补码系统解决了表示负数的补码系统中存在的多重表示0的问题以及需要尾部进位的问题。
请访问https://en.wikipedia.org/wiki/Signed_number_representations
欲了解更多信息, -进行访问
https://en.wikipedia.org/wiki/End-around_carry
One satisfactory answer of why Two2's Complement is used to represent negative numbers rather than One's Complement system is that
Two's Complement system solves the problem of multiple representations of 0 and the need for end-around-carry which exist in the One's complement system of representing negative numbers.
For more information Visit https://en.wikipedia.org/wiki/Signed_number_representations
For End-around-carry Visit
https://en.wikipedia.org/wiki/End-around_carry
这里尚未提到的二进制补码表示的一个主要优点是,二进制补码和、差或乘积的低位仅依赖于操作数的相应位。 -1 的 8 位有符号值是
11111111
的原因是,从任何其他整数中减去最低 8 位为00000001
的任何整数。最低 8 位为0000000
将产生一个最低 8 位为11111111
的整数。 从数学上讲,值 -1 将是一个由 1 组成的无限字符串,但特定整数类型范围内的所有值在某一点之后要么全为 1,要么全为 0,因此计算机可以方便地“符号扩展”数字的最高有效位,就好像它代表无限数量的 1 或 0。补码几乎是唯一一种在处理大于二进制机器自然字大小的类型时效果很好的有符号数表示形式,因为在执行加法或减法时,代码可以获取每个操作数的最低块,计算每个操作数的最低块。结果,并存储它,然后加载每个操作数的下一个块,计算结果的下一个块,并存储它,等等。因此,即使是需要所有加法和减法通过单个 8 位寄存器的处理器可以相当有效地处理 32 位有符号数(当然,比 32 位寄存器慢,但仍然可行)。
当使用 C 标准允许的任何其他有符号表示形式时,结果的每一位都可能受到操作数的任何位的影响,因此必须立即在寄存器中保存整个值,或者在计算后添加额外的值至少在某些情况下,该步骤需要读取、修改和重写结果的每个块。
A major advantage of two's-complement representation which hasn't yet been mentioned here is that the lower bits of a two's-complement sum, difference, or product are dependent only upon the corresponding bits of the operands. The reason that the 8 bit signed value for -1 is
11111111
is that subtracting any integer whose lowest 8 bits are00000001
from any other integer whose lowest 8 bits are0000000
will yield an integer whose lowest 8 bits are11111111
. Mathematically, the value -1 would be an infinite string of 1's, but all values within the range of a particular integer type will either be all 1's or all 0's past a certain point, so it's convenient for computers to "sign-extend" the most significant bit of a number as though it represented an infinite number of 1's or 0's.Two's-complement is just about the only signed-number representation that works well when dealing with types larger than a binary machine's natural word size, since when performing addition or subtraction, code can fetch the lowest chunk of each operand, compute the lowest chunk of the result, and store that, then load the next chunk of each operand, compute the next chunk of the result, and store that, etc. Thus, even a processor which requires all additions and subtractions to go through a single 8-bit register can handle 32-bit signed numbers reasonably efficiently (slower than with a 32-bit register, of course, but still workable).
When using of the any other signed representations allowed by the C Standard, every bit of the result could potentially be affected by any bit of the operands, making it necessary to either hold an entire value in registers at once or else follow computations with an extra step that would, in at least some cases, require reading, modifying, and rewriting each chunk of the result.
有不同类型的表示形式:
- 无符号数表示法仅用于表示正数
- 有符号数表示法用于表示正数和负数。 在有符号数字表示中,MSB 位代表符号位,其余位代表数字。 当MSB为0时表示数字为正,当MSB为1时表示数字为负。
有符号数字表示的问题是 0 有两个值。
补码表示的问题是 0 有两个值。
但是如果我们使用二进制补码表示,那么0 只有一个值,这就是为什么我们用二进制补码形式表示负数。
来源:为什么负数以二进制补码形式存储 bytesofgigabytes
There are different types of representations those are:
-Unsigned number representation used to represent only positive numbers
-Signed number representation used to represent positive as well as a negative number. In Signed number representation MSB bit represents sign bit and rest bits represents the number. When MSB is 0 means number is positive and When MSB is 1 means number is negative.
Problem with Signed number representation is that there are two values for 0.
Problem with one's complement representation is that there are two values for 0.
But if we use Two's complement representation then there will only one value for 0 that's why we represent negative numbers in two's complement form.
Source:Why negative numbers are stored in two's complement form bytesofgigabytes