Origin code, reverse code, and supplementary code

Source: Internet
Author: User

1. Why do we prefer hexadecimal?

The hexadecimal number is 16, 1, and 2, and 1. 2 ^ 4 = 16.

Hexadecimal to one digit corresponds to binary to four digits (hexadecimal 10 = binary 1 0000, hexadecimal 100 = binary 1 0000 ).

Therefore, the hexadecimal number (such as 2a9) can be converted into binary values by bit: 2 (0010) A (1010) 9 (1001 ). Each digit corresponds to four binary digits.

That is, 2a9 = 0010 1010 1001

Therefore, when we see the hexadecimal number fffffff3, we can immediately and conveniently reflect its binary value.

The hexadecimal description is displayed before the value:

Binary: (Binary) 0000 0011

Octal: (octal) 325

Decimal: 289

Hexadecimal: (HEX) FFFF fff3

2. storage of data in memory

Int I = 3;

Taking a 16-bit system as an example, I occupies two bytes in the memory. The structure is as follows: (Binary) 0000 0000 0000 0011

For convenience, we set the system to 8-bit. Int occupies only one byte. That is, the storage structure of int I = 3 is: (Binary) 0000 0011

3. Overflow and the concept of "modulo"

 

As can be seen from the above, if int occupies one byte, the value range of the data that can be stored is (Binary) 0000 0000-> (Binary) 1111 1111, or (decimal) 0-> (decimal) 255

If you want to add two numbers beyond this range, it will "overflow ".

For example, in this storage, (decimal) 4 + (decimal) 254 =?

From the binary perspective:

(Binary) 0000 0100 = (decimal) 4

(Binary) 1111 1110 = (decimal) 254

------------------------------------- +

(Binary) 1 0000 0010 = (decimal) 258

Because it can only store 8 bits, the overflow 1 has to be lost! So there is only (Binary) 0000 0010.

That is to say: (decimal) 4 + (decimal) 254 = (decimal) 2 (discard the strange result of overflow! This is the root cause of negative calculation !)

The number of overflow dropped, that is, (Binary) 1 0000 0000 = (decimal) 256, is called "modulo ".

It can be understood as the length of the bucket (from 0 to 255, and the length is exactly 256 ). When the sum of two numbers exceeds the length of the bucket, we take the remainder of the modulus (for example, 258 mod 256 = 2 ).

4. Why do I need to complete the code?

According to the preceding assumption, int occupies only one byte, that is, the 8-bit storage structure. Obviously, only positive numbers in the specified range can be stored here, and only addition and subtraction operations can be performed (the results can only belong to this range, and the overflow will be lost ).

What should I do with negative numbers?

Obviously, computers do not recognize negative numbers. Negative Numbers can also be interpreted as subtraction.

Based on the previous discard overflow operation, we have:

(Decimal) 4 + (decimal) 254 = (decimal) 2

Obviously, we also have:

(Decimal) 4-(decimal) 2 = (decimal) 2

That is to say, when the overflow operation is discarded:

+ (Decimal) 254 =-(decimal) 2

Haha, do you mean + 254 =-2 ?!

Yes, this is the case in this discard overflow algorithm. (It can be understood that 8 hours (+ 8) forward of the clock and 4 hours (-4) backward are the same results)

That is to say, if a + B = "modulo" within the storage range, + A =-B;-A = + B under the condition that overflow is discarded

Then we can directly use + 254 to represent-2! (Because they have the same effect in Operations !)

So (Binary) 1111 1110 can both represent + 254 or-2!

But how can I let the computer know that the value I want is-2, instead of + 254?

Add a flag in front of it. For example, adding 0 indicates that we want to get + 254, and adding 1 indicates that we want to get-2.

But our storage is only 8 bits! Finally, we had to take out the first value in the storage as the symbol bit (0 indicates a positive number, 1 indicates a negative number), and the remaining seven digits are used to store the value, as follows:

0 000 0011

The first 0 is the symbol bit. Result: The symbol bit is 0, and the value is (Binary) 000 0011.

Well, in the original 8-bit storage structure, the value can be 0 => 255. To support negative numbers, the first digit is used as the symbol bit, the value range that can be expressed is-128 => + 127 (1 000 0000 =-128 ).

Obviously, the "modulo" of the numeric value has also changed to 128. In the same operation as above, the positive number equal to-2 also changes to + 126.

Therefore, the-2 is written as binary according to-2 = + 126:

(1). the symbol is retained in the symbol bit, that is, the symbol bit = 1 (0 indicates a positive number, and 1 indicates a negative number)

(2). The value is directly + 126, that is, the value is 111 1110.

Result:-2 = 1 111 1110

While the binary representation of positive numbers is much simpler and more intuitive: + 126 = 0 111 1110

In this way, we only need to put the eight-bit storage structure out of the first to represent the positive/negative symbols, we can store the positive/negative numbers, and perform addition and subtraction (negative numbers are represented by the corresponding positive numbers, the calculation result is the same, but the subtraction operation adds a negative number !).

Original code:

Convert any number to binary. Then press "0" to indicate a positive number, and "1" to indicate a negative number. "add a symbol, which is the original code! For example:

(Decimal) 2 original code = (Binary) 0 000 0010

(Decimal)-2 original code = (Binary) 1 000 0010

Anti-code:

A positive number is equal to the original code.

(Decimal) 2 anti-code = (Binary) 0 000 0010

Negative number, as the name suggests, is to reverse the part of the original code value. For example:

(Decimal)-2 anticode = (Binary) 1 111 1101

Complement:

A positive number is equal to the original code.

(Decimal) 2 anti-code = (Binary) 0 000 0001

Negative number, equals to the back code plus 1.

(Decimal)-2 anticode = (Binary) 1 111 1110

Do you understand? This is not exactly the value of-2 that we used to represent with + 126!

Yes, according to the previous analysis, the positive numbers that are equal to negative numbers, the sum of their absolute values is exactly equal to the "modulo" (which can be understood as eight hours forward (+ 8 ), this is the same effect as 4 hours (-4 ). For a binary number, first take the inverse, add 1, and then add the original number, which is exactly a process for "modulo, the process of finding + 126 in decimal format through-2.

Therefore, negative numbers are represented by supplementary codes in the computer.

For zero, the complement code of (Binary) 0 000 0000 is (Binary) 1 000 0000, so (Binary) 1 000 0000 is-128. Therefore, the range of negative numbers is-128 => + 127.

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.