Problem analysis of Java original code inverse code and complement code

  • 2020-04-01 01:50:51
  • OfStack

1. Definition of original code, inverse code and complement code

1. The original code

Take the highest bit as the sign bit (0 for positive, 1 for negative) and the rest as the absolute value of the value itself (in binary).
For simplicity, we use 1 byte to represent an integer.
        The original code of +7 is: 00000111
        The original code of -7 is: 10000111

2. Radix-minus-one complement

If a number is positive, its inverse code is the same as the original code. If a number is negative, the sign bit is 1, and the rest are inverted.
For simplicity, we use 1 byte to represent an integer:
        The inverse of +7 is: 00000111
        The inverse of minus 7 is: 11111000

3. The complement

Complement: if a number is positive, its original code, inverse code, complement code is the same; If a number is negative, the sign bit is 1, the rest of the digits are inverted, and then the whole number is added by 1. For simplicity, we use 1 byte to represent an integer:
The complement of +7 is: 00000111
The complement of -7 is: 11111001

Given the complement of a negative number, convert it to decimal number, steps:
          1. Invert each of you first;
          2. Convert it to decimal number;
          3, plus the minus sign, minus 1.
          Such as:
          11111010, the highest bit is 1, is a negative number, so if I invert it to 00000101, then I convert it to a decimal number which is 5, plus a minus sign which is minus 5, minus 1 which is minus 6.

Frequently asked questions

I'm going to take int a=232; Why is it negative when strong to byte??

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/201304271041323.jpg ">


Related articles: