Why prefer two's complement over sign-and-magn

2019-01-03 18:58发布

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!

18条回答
冷血范
2楼-- · 2019-01-03 19:46

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.

查看更多
\"骚年 ilove
3楼-- · 2019-01-03 19:46

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.

查看更多
干净又极端
4楼-- · 2019-01-03 19:51

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.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-03 19:52

You can watch Professor Jerry Cain from Stanford explaining the two's complement, in the second lecture (the explanation regarding the 2's complement starts around 13:00) in the series of lectures called Programming Paradigms available to watch from Standford's YouTube channel. Here's the link to the lecture series: http://www.youtube.com/view_play_list?p=9D558D49CA734A02.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-03 19:52

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-03 19:55

Wikipedia says it all:

The two's-complement system has the advantage of not requiring that the addition and subtraction circuitry examine the signs of the operands to determine whether to add or subtract. This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in ones'-complement systems.

In other words, adding is the same, wether or not the number is negative.

查看更多
登录 后发表回答