Analysis of problems with original code, reverse code, and complement in Java

Source: Internet
Author: User

1. Definition of source code, reverse code, and complement code

1. Original code

Use the highest bit as the sign bit (0 represents positive, 1 represents negative), and the rest represent the absolute value of the value (in binary format ).
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. Anti-code

If a number is positive, its anticode is the same as the original code. If a number is negative, the symbol bit is 1, and the rest of the codes are reversed.
For simplicity, we use 1 byte to represent an integer:
+ The Anti-Code of 7 is 00000111.
-The Anti-Code of 7 is 11111000.

3. Complement

Complement: If a number is positive, the original code, the reverse code, and the complement code are the same; if the number is negative, the signed bit is 1, and the rest are reversed, then the entire number plus 1. For simplicity, we use 1 byte to represent an integer:
+ 7 completion code: 00000111
-7: 11111001

To convert a negative complement to a decimal number, follow these steps:
1. Get the opposite information for you first;
2. convert it to a decimal number;
3. Add a negative number and subtract 1.
For example:
11111010, the highest bit is 1, is a negative number, first take the inverse 00000101 for everyone, convert to the decimal number 5, add the negative number-5, then minus 1-6.

2. FAQs

Why is int A = 232 negative when I convert int A = to byte type ???

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.