Modulo and Complement

Modulo: The number of all data in the case of no carry (Baidu entry did not find, my own understanding).
For example, the commonly used decimal number modulus is 10, and the number of data is 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The mode of the hour is 12, and the mode of the minutes and seconds is 60. The modulo applied to binary on a computer is 2. The modulo of the angle operation is 360.
The complement code is equal to the (N times) modulo minus the original code. The complement of the complement is equal to the original.
The complement and inverse of positive numbers are the same as the original code. The inverse, complement, and original sign bits of negative numbers are all 1.

Small method:
quickly convert the negative complement to decimal (one byte as an example):
1. The first 0 is encountered from the left (the Nth bit from the right), high=2^N
2. The following is 1 Calculate their values, sum, low=sum(2^M, 2^P,...)
3. High-low, the result is negative.
For example, the result of 1000,0000 is the negative of 128; the result of 1110,1001 is 2^5-2^3-2^0=23, which is negative, and the result of 1111,1111 is -1.


Given a positive number, find its complement: such as ~5=-6, ~-6=5. A formula ~n=-(n+1) can be applied, the reason: the weight of each bit in binary is a proportional sequence such as 1 2 4 8, q=2, and the sum of the proportional sequence = a1(1-q^n)/ 1-q, which is established according to the above small method formula.


<<1 is a left shift, which is equivalent to *2, >>1 is a right shift, which is equivalent to dividing by 2. Note that jvm is only valid for 32 and 64 bits, byte and short will be converted to int by default and then shifted. >> is a signed shift, that is, the sign bit also participates in the shift, and the highest bit is supplemented with 1, so -1>>3 is still -1, >>> the sign bit does not participate in the shift, the highest bit is supplemented with 0, so -1 >>>30=3.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801075&siteId=291194637