Вопрос по – Что такое «Дополнение 2»?

384

Error: User Rate Limit ExceededstrugglingError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded.

Error: User Rate Limit Exceededcommunity wikiError: User Rate Limit Exceeded

Error: User Rate Limit Exceededa clear and concise definitionError: User Rate Limit Exceeded

Ваш Ответ

13   ответов
1

enter image description here

It is a useful convention. The same circuits and logic operations that add / subtract positive numbers in binary still work on both positive and negative numbers if using the convention, that's why it's so useful and omnipresent.

Imagine the odometer of a car, it rolls around at (say) 99999. If you increment 00000 you get 00001. If you decrement 00000, you get 99999 (due to the roll-around). If you add one back to 99999 it goes back to 00000. So it's useful to decide that 99999 represents -1. Likewise, it is very useful to decide that 99998 represents -2, and so on. You have to stop somewhere, and also by convention, the top half of the numbers are deemed to be negative (50000-99999), and the bottom half positive just stand for themselves (00000-49999). As a result, the top digit being 5-9 means the represented number is negative, and it being 0-4 means the represented is positive - exactly the same as the top bit representing sign in a two's complement binary number.

Understanding this was hard for me too. Once I got it and went back to re-read the books articles and explanations (there was no internet back then), it turned out a lot of those describing it didn't really understand it. I did write a book teaching assembly language after that (which did sell quite well for 10 years).

5

1010101010101010+1=01011

304

0000 = 0
0001 = 1
0010 = 2
...
1111 = 15

Sign Magnitude and Excess Notation

0000 = +0
0001 = +1
0010 = +2
...
1000 = -0
1001 = -1
1111 = -7

0010
1001 +
----

?

Convert Decimal to Two's Complement

Convert the number to binary (ignore the sign for now) e.g. 5 is 0101 and -5 is 0101

If the number is a positive number then you are done. e.g. 5 is 0101 in binary using twos complement notation.

If the number is negative then

3.1 find the complement (invert 0's and 1's) e.g. -5 is 0101 so finding the complement is 1010

3.2 Add 1 to the complement 1010 + 1 = 1011. Therefore, -5 in two's complement is 1011.

 2  =  0010
 -3 =  1101 +
 -------------
 -1 =  1111
Converting Two's Complement to Decimal

The number starts with 1, so it's negative, so we find the complement of 1111, which is 0000.

Add 1 to 0000, and we obtain 0001.

Convert 0001 to decimal, which is 1.

Apply the sign = -1.

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
3

1111 - 0101 = 1010

2

Error: User Rate Limit ExceededError: User Rate Limit Exceeded this article explain it graphically.

Error: User Rate Limit Exceeded
18

-3.

-3!

What I realized:

3 * 0) + (22 * 1) + (21 * 0) + (20 * 1) = 5.

Do exactly the same concept with a negative!(with a small twist)

3 * 1 = 83 * 1) = -8.

-8 + (22 * 1) + (21 * 0) + (20 * 1) = -3

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1

To avoid multiple representations of 0 To avoid keeping track of carry bit (as in one's complement) in case of an overflow. Carrying out simple operations like addition and subtraction becomes easy.

0

  // in C++11
  int _powers[] = {
      1,
      2,
      4,
      8,
      16,
      32,
      64,
      128
  };

  int value=3;
  int n_bits=4;
  int twos_complement = (value ^ ( _powers[n_bits]-1)) + 1;
Error: User Rate Limit Exceeded
111

are


2345

23 + 32 + 41 + 50.

01
1111

13 + 12 + 11 + 10

complement


00001111

07 + 06 + 05 + 04 + 132 + 11 + 10


11110000

11110001

111110001
17 + 16 + 15 + 14 + 032 + 01 + 10
7



10

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
4

00001010
11110100
-----------------
11111110  
561

for zero, use all 0's. for positive integers, start counting up, with a maximum of 2(number of bits - 1)-1. for negative integers, do exactly the same thing, but switch the role of 0's and 1's (so instead of starting with 0000, start with 1111 - that's the "complement" part).

0000 - zero 0001 - one 0010 - two 0011 - three 0100 to 0111 - four to seven

3-1 = 7.

1111 - negative one 1110 - negative two 1101 - negative three 1100 to 1000 - negative four to negative eight

10000000

Distinguishing between positive and negative numbers

10

1000

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

000111110001.

000011110000

0001 + 11110000.

.

13

Error: User Rate Limit Exceeded

00
01
02
..

Error: User Rate Limit Exceeded

98
99
00

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded99.

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededten's complementError: User Rate Limit Exceededtwo's complementError: User Rate Limit Exceeded

Error: User Rate Limit Exceededjust worksError: User Rate Limit Exceeded

Похожие вопросы