Mutual conversion of original code inverse code and complement code

Mutual conversion of original code inverse code and complement code

The conversion of the original code's inverse code and complement code is relatively simple and basic. I learned it when I was learning java before, but I forgot it, forgot it! ! ! , Later, I learned the shift operator, and I was a little confused after shifting left, shifting right, unsigned shifting right.

Original code, inverse code, complement code
The first bit in binary is the sign bit, 0 means a positive number, and 1 means a negative number.
Take, for example, an eight-bit binary number.

  1. Original code The binary original code of the decimal
    number positive 1 [+1] original = 0000 0001 the binary original code of
    the decimal number negative 1 [-1] original = 1000 0001
    The first bit is the sign bit . Because the first bit is the sign bit, so
    Value range of 8-bit binary number: [1111 1111 , 0111 1111]
    corresponds to the range of decimal [-127 , 127].

  2. The way of expressing one's complement and one
    's complement is:
    1. The one's complement of a positive number is itself
    2. The one's complement of a negative number is based on the original code, the sign bit remains unchanged, and the rest of the bits are reversed.
    [+1] = [0000 0001] Original = [0000 0001] Inverse
    [-1] = [1000 0001] Original = [1111 1110] Inverse

  3. Complement code
    The expression method of complement code is:
    the complement code of a positive number is itself,
    the complement code of a negative number is based on the original code, the sign bit remains unchanged, the rest of the bits are reversed, and finally +1 . Based on +1)
    [+1] = [0000 0001] original = [0000 0001] inverse = [0000 0001] complement
    [-1] = [1000 0001] original = [1111 1110] inverse = [1111 1111] complement

  • Summary
    In the case of knowing the original code of a number:
    Positive number: Inverse code, the complement code is its own
    negative number: Inverse code means that the high-order sign bit remains unchanged, and the remaining bits are inverted. Complement code: Inverse code +1

Exercise
-A (binary number) converts the original code to complement code, first subtract one and then invert or invert first and then add 1.
-A (binary number) complement code is converted to the original code: bitwise inversion plus 1 or minus one and then bitwise inversion Note that the first sign bit remains unchanged.
The sign bit is the first bit, 1 is negative and 0 is positive. Example of the size of the value that does not represent
:
Q: 4’s complement: 0100 -4’s complement: 1100?
Answer: The original code of -4 is 1000 0100; the inverse code (the sign bit of the original code remains unchanged, and the bitwise inversion) is 1111 1011; the complement code (inverse code plus one) is 1111 1100 in eight-bit binary (assumed to be eight-
bit ): 4’s complement code is: 0000 0100 -4’s complement code is: 1111 1100
Detailed explanation:
-4 original code is converted into -4’s complement code conversion
method 1 : 1000 0100 minus one to get 1000 0011 bitwise inversion 1111 1100
Method 2 : Bitwise inversion of 1000 0100 to get 1111 1011 plus one to get 1111 1100
-4's complement is transformed into the original code bitwise inversion but the sign bit remains unchanged
Method 1 : 1111 1100 Bitwise inversion is 1000 0011 plus One 0000 0100 take the negative number to get -4
Method 2 : Subtract one from 1111 1100 to get 1111 1011 bitwise inversion 1000 0100 take the negative number to get -4
If you can’t remember, remember, complement code = original code bitwise inversion and then +1, original code = complement code minus one and then bitwise inversion.
Key points:
1. For positive numbers, the inverse and complement of the original code are the same. The above rules are for negative numbers.
2. The original codes of positive numbers and negative numbers only differ by the value of the sign bit. If they are different, positive numbers are 0 and negative numbers are 1.

Guess you like

Origin blog.csdn.net/qq_41117240/article/details/99093093