Source code, reverse code and complement code

Source: Internet
Author: User

 

1. Representation of the source code, reverse code, and complement code

 

(1) original code: directly add a sign before the value.

 

For example, the symbol bit value

 

[+ 7] original = 0 0000111 B

 

[-7] original = 1 0000111 B

 

Note: a. The original code of the number 0 has two forms:

 

[+ 0] original = 00000000B [-0] original = 0000000b

 

B. 8-bit binary original code representation range:-127 ~ + 127

 

(2) Anti-code:

 

Positive: the inverse code of a positive number is the same as the original code.

 

Negative number: the inverse code of a negative number. The symbol bit is "1", and the value part is reversed by bit.

 

For example, the symbol bit value

 

[+ 7] inverse = 0 0000111 B

 

[-7] Reverse = 1 1111000 B

 

Note: a. There are two anti-code forms for the number 0, that is

 

[+ 0] = 00000000B

 

[-0] anti = 111111b

 

B. The range of 8-bit binary anticode:-127 ~ + 127

 

(3) representation of the complement

 

12) representation of the complement:

 

Positive: the positive complement is the same as the original code.

 

Negative number: the complement of the negative number is the sign bit "1", the value part is reversed by bit and then added 1 to the last bit. That is, "anti-code + 1 ".

 

For example, the symbol bit value

 

[+ 7] fill = 0 0000111 B

 

[-7] fill = 1 1111001 B

 

The complement code is an important encoding method in a microcomputer. Note the following:

 

A. After completing the code, you can easily convert the subtraction operation into an addition operation, which simplifies the operation process. The positive value is the true value of the number it represents, while the negative value is not the true value of the number it represents. The result is still the complement code.

 

B. Unlike the source code and reverse code, the value 0 has only one complement code, that is, [0] fill = 00000000B.

 

C. If the length is 8 characters, the value range of the complement code is-128 ~ + 127; when performing the complement operation, note that the result should not exceed the range of the number that can be expressed by the complement.

 

2. Conversion between source code, reverse code, and complement code

 

Conversion is not required because the original code, complement code, and reverse code representation of positive numbers are the same.

 

Here, only negative numbers are analyzed.

 

(1) obtain the source code.

 

For example, if the original code of X is 10110100B, try to obtain the complement and reverse code of X.

 

Solution: It is determined by [X] original = 10110100B, and X is a negative number. When the anticode is obtained, the symbol bit remains unchanged, and the value part returns by bit. When the value is obtained, 1 is added to the end of the anticode.

 

1 0 1 1 0 1 0 0 original code

 

 

 

1 1 0 0 1 0 1 1 1 anticode, the symbol bit remains unchanged, and the value bit is reversed.

 

1 + 1

 

1 1 0 0 1 1 0 0 Complement

 

Therefore: [X] fill = 11001100B, [X] Reverse = 11001011B.

 

(2) If the source code is known, obtain the source code.

 

Analysis: According to the inverse process of finding a negative complement code, the value part should be a percentile minus 1, and then take the inverse. However, for the binary number, the result obtained by first subtracting 1 and then obtaining the inverse is the same as that obtained by first obtaining the inverse and then adding 1. Therefore, you can still use the method of obtaining the inverse and adding 1.

 

For example, if you know the source code of a certain number of X, try to obtain the source code.

 

Solution: [X] supplements = 11101110B, and X is a negative number. When the original code is obtained, the symbol bit remains unchanged. The value is reversed by bit, and 1 is added to the last digit.

 

1 1 1 0 1 1 0 Complement

 

 

 

1 0 0 1 0 0 0 1 the symbol bit remains unchanged, and the value bit is reversed.

 

1 + 1

 

1 0 0 1 0 1 0 0 original code

This article is from the "keep running" blog

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.