Source code, reverse code, and complement code

Source: Internet
Author: User
I. Definition of the original code

The original, reverse, and supplementary codes are Binary Fixed-point representation of numbers in the computer.

The highest bit of the original code representation is the sign bit. 0 indicates positive, and 1 indicates negative. The specific definitions are divided into decimals and positive numbers. Here we will only discuss the definitions of integers:

Assume that one byte is used to represent integer x, so a total of 256 different x values can be used:

When X is in [0,127], the original code of X is X;

When X is in [-127,-0], the original code of X is 128-x.

Note the following:

Zero indicates two values: Positive and Negative. The values are 0000 0000 and 1000 0000, respectively;

The maximum positive number is 127, the minimum negative number is-127, plus positive and negative zero, a total of 256 numbers;

+ 0 0000 0000

+ 1 0000 0001

+ 2 0000 0010

+ 127 0111 1111

-0 1000 0000

-1 1000 0001

-127 1111 1111

 

Ii. Anti-Code definition

Positive anticode: same as the original code

Negative Inverse code: the highest bit symbol bit remains unchanged, and the value is 1. The others are reversed by bit, that is, 0 is changed to 1, 1 is changed to 0.

The representation range is the same as that of the original code: [-127, + 127]. The representation of + 0 and-0 is also different, which are 0000 0000 and 1111 1111, respectively.

 

Iii. Definition of supplemental code

Positive complement: same as the original code

Complement of negative numbers: reverse code + 1

The expression of positive and negative Zeros is exactly the same: 0000 0000. The reason is very simple: the anticode of positive and negative is the original code of zero, 0000 0000, which is no problem; the complement of negative zero is its anti-code plus 1, and its anti-code is mentioned above, it is 1111 1111, it should be 1 0000 0000 after adding 1, however, since we have declared that we use one byte to represent an integer, the first of the highest bits will be removed, and the result is 0000 0000.

Its representation range has changed: [-128, + 127]. Why is it possible to represent-128? The reason is as follows:

One byte can represent 256 different numbers, but now the positive and negative zeros are the same, so there must be one remaining number, which is 1000 0000, which can be verified: [-127, + 127] does not have any number of complement codes. The reason is very simple. We can calculate the original code based on this complement code. The process is as follows:

First, assume it is a positive complement. Because the original code, reverse code, and complement code of a positive number are identical, if it is a positive number, its original code is its complement code, so the original code is also 1000 0000, however, according to the definition of the original code, the highest bit is 1, indicating a negative number, which is self-contradictory. Therefore, if it is incorrect, we can conclude that it is not a positive number;

Next, assume it is a negative complement. If it is reduced by 1, it will get its anti-code 0111, which is also incorrect, because the highest bitwise of a negative number is also 1, which is also self-contradictory, therefore, it is not a negative original code;

At last, it can only be a zero complement, but we already know that the positive and negative complement codes are the same, both of which are 0000 0000, so it is not a zero complement.

To sum up, it is not a complement of any number in [-127, + 127]. Isn't it a waste? To avoid waste, we specifically stipulate that 1000 0000 is a-128 complement.

+ 0 0000 0000

+ 1 0000 0001

+ 2 0000 0010

+ 127 0111 1111

-128 1000 0000

-127 1000 0001

-1 1111 1111

Therefore, in the world of complement code, it gradually increases from 0 until the largest positive number, for example, if you add 1, then jump to the smallest negative number, and then gradually increases, until it becomes the largest negative number.

Source code change diagram complement code change diagram

In the computer, the numbers are all represented by supplementary codes. The two reasons are as follows: first, the original codes and reverse codes represent different zeros. Therefore, this is not a mathematical logic and is not suitable for number calculation, in addition, they also need to perform special processing on the symbol bit, while the complement code first is unique for the zero expression. Secondly, it does not need to perform special processing on the symbol bit, that is, the symbol bit can be directly involved in the operation, subtraction can also be processed as addition.

Algorithm of complement: [A + B] fill = [a] fill + [B] fill

Therefore, you can export: [A-B] fill = [A + (-B)] fill = [a] fill + [-B] fill

The question about how to calculate the complement of negative numbers, including carry or overflow involved in it, should be further studied later.

 

Iv. Discussion of int type variables in Java

The numbers in Java are all signed numbers. Int variables are expressed in four bytes. Therefore, the maximum value is 31 times of 2 minus 1, and the minimum value is 31 times of negative 2.

 

 

 

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.