Complement Analysis of Negative Number Remainder

Complement Analysis of Negative Number Remainder

The basic concept of complement

For computer processor systems, when doing calculations, the original code is converted to complement, and after the calculation, the final result is converted to the original code. Only the unit of the adder is implemented in the CPU, and there is no unit of the subtractor. X original code -Y original code = X complement code + (-Y) complement code .
The complement of a positive number is itself. The complement of a negative number is the highest bit unchanged (1). After the other bits are inverted, the overall lowest bit is added by 1 to get. Such as

7 == 00000111 (binary 8-bit original code) = 00000111 (binary 8-bit complement)
-7 = 10000111 (binary 8-bit original code) = 11111001 (binary 8-bit complement)

The complement of the remainder is special in it, and the final result of the remainder must be a positive number, so the complement is the original code.

The remainder of positive complement

7/4 = 1 (quotient), 7%4 = 3 (remainder)
The part of the quotient is equivalent to two complements to the right,
000001 11 (binary 8-bits complement)
in the high-order 0,
00000001 (binary 8-bits complement) )
Converted to the original code, it is still
00000001 (binary 8-bit original code)
that is the decimal number 1

The remaining part is 11 (binary original code)
according to the defined data length (in this case, byte, that is, 8 bits), the high-order complement is
00000011 (binary original code)
and
00000011 (binary complement),
so the remaining decimal is 3

The remainder of negative complement

Here is a bit more complicated, first is the question
-7/4 =? (Quotient), -7%4 =? (More than)

Let's look at the analysis method of quotient, x satisfies (n-1) d≤ x <n d, d is a positive integer greater than 0, and n is a positive integer greater than 0.
(n*dx) /d = (-x+n*d)/d = (-x)/d + (n*d)/d = (-x)/d + n

Substituting n = 2, d = 4, x = 7, can be obtained
(n-DX *) * = 4-7 = 2. 1
. 1 = (the -X-) / n-D + = (the -X-) / 2 + D
conversion to give
( -x)/d = -1
ie (-7/4 = -1)

Finding the quotient is actually the division of one's complement. The complement of -7 is
11111001 (two's complement)
divided by 4 is equal to two shifts to the right, the highest bit is 1, so add 2 bits 1
11111110 (two's complement)
will complement Converted to the original code is
10000001 (binary original code)
that is decimal -1

Therefore, the quotient of (-x)/d is equivalent to x/d and then negative.

Let's look at the analysis method for remainder again, x satisfies (n-1) d≤ x <n d, d is a positive integer greater than 0, and n is a positive integer greater than 0.
(n*dx) %d = ((-x+n*d)%d)%d = ((-x)%d + (n*d)%d) %d = ((-x)%d) %d = (-x)%d
substituting n=2, d=4, x=7, you can get
(n*dx) = 2*4-7 = 1
1 = (-x)%d = (-7) %4

That is (-7)%4 = 1, which is different from the situation of seeking quotient:

  1. Negative remainder is not negative
  2. Negative remainder does not correspond to the result of positive remainder and then take a negative value

From the analysis of one's complement operation, the complement of -7 is
11111001 (two's complement)
divided by 4 equal to two bits shifted to the right
111110 01 (two's complement)
01 is shifted out and treated as the complement of a positive number, according to the defined data length (Here is a byte, that is, 8 bits) The high bit is supplemented with 0 to get
00000001 (two's complement) of
course, the original code is also
00000001 (the original binary code)

From the above analysis, it can be seen that for the remainder of negative numbers, in fact -x%d = d-(x%d) .

-End-

Guess you like

Origin blog.csdn.net/hwytree/article/details/103224586