(1) Representation of basic data (integer)

Source: Internet
Author: User

(1) Representation of basic data (integer)

1. Integer Representation

1.1 original code, reverse code and complement code

With regard to the concept of source code, anti-code, and complement code, no one with a computer base will be unfamiliar, so we will not mention it here.

We all know that the data in the memory is expressed as a binary value of 01. An integer is an unsigned integer, followed by a signed integer, that is, a positive integer and a negative integer. The highest bit indicates the symbol bit. When the data in the memory is interpreted as the number of unsigned digits, the highest bit is also involved in the calculation. When interpreted as a signed number, the highest bit does not participate in the calculation. It only represents the symbol, 0 represents a positive integer, and 1 represents a negative integer. Integers exist in the memory in the form of a complement code, and the complement code of a positive integer is equal to its original code. This is not much explained. The following describes the expression of a negative integer in the memory.

1.2 complement Concept

As to why integers are expressed in the memory in the form of supplementary codes, this question has been confusing me for a long time when I was a beginner in a language, I hope that the text below will make friends who are equally confused feel enlightened.

First, let's talk about the concept of Code complement. Isn't the concept of Code complement easy yet? Everyone knows: Code complement = reverse (Anti-code) + 1 of the original code, yes, my friend is right, but this is just a formula for calculating the Code complement, not its concept.

What is the definition and what is the complement? ----The complement is to subtract the absolute value of this number from 0.Remember the red mark, which is the key to making us suddenly open. At the same time, we should also note that the concept of Code complement is closely related to the machine font length (the number of digits that can be processed at a time when the CPU performs an integer operation,It does not make any sense to fill in the code without specifying the machine font length.. Next we will use the complement concept to derive the formula "complement = retrieve inverse + 1.

1.3 derivation of the complement Formula

let's find the-5 complement. According to the concept, we have "-5 (fill) = 0-|-5 | = 0-5", that is, "-5 (fill) + 5 = 0 ". In 32-bit machine characters, there are "0 xfffffffb + 5 = 0" (here the carry is lost, so the complement code is closely related to the specific machine's character length ), therefore, 0xfffffffb is the completion code of-5 32-bit machine Character length. (In subsequent discussions, we will assume that the machine word length is 32 characters.) Likewise, 0xfffffffb has a highest bit of 1, so it represents a negative number, so we can calculate the absolute value of 0-06 xfffffffb, "00000005" means 5. its height overflows . We can use a calculator to demonstrate it, In the calculator that comes with windows, we choose four-character length, that is, 64-bit. Calculate "0-0xfffffffb = ffffffff00000005", then only the 32-bit low is "00000005 ".

Next, we know thatThe absolute value of any negative integer plus its anticode is equal to 0 xfffffff (note that only the negative integer is used here, and the original anti-complement three codes of the positive integer are the same)If 0 xffffffff + 1, the loss of carry is equal to 0. So we have "| x | + x (inverse) + 1 = 0". After a simple shift, we get "X (inverse) + 1 = 0-| x | ". How is "0-| x |" not exactly the definition of our complement? Therefore, we can apply the formula to facilitate the computation during code complementing:

"Complement = reverse + 1 ".

1.4 significance of Code Completion

I believe that, based on the formula above, you must have a new understanding of the complement code. So let's take a look at the significance of the complement code. We know that computers only perform addition. What should we do when we need to perform subtraction? Let's continue with the derivation.

Suppose we want to calculate "X-Y", then we can be expressed as "x + (0-| Y |)", and 0-| Y |, is not exactly the definition of the complement, then we get "x-y = x + y (fill)", so the meaning of the complement code is to convert the subtraction into addition! For example, if we calculate "8-5", we first convert it to "8 + (0-|-5 | )", next, "0-|-5 | =-5 (fill) = 0 xfffffffb", and finally "8 + 0 xfffffffb = 00000003" (carry loss), so 8-5 = 3. Oh, do you think it's suddenly clear? I have to admire the wisdom of our predecessors. If a simple carry loss occurs, the subtraction is done.

Here, let's talk about the number 0x8000000. The binary value is 1000 0000 0000 0000 0000 0000 0000 0000. The highest bit of this number is 1. When we interpret it as a signed number, then it indicates negative zero. However, Zero is not divided into positive and negative values. What should we do? Don't worry,0x80000000 not only indicates negative zero, but also "0x80000001-1 ",-2147483648. This explains why a signed number has one more range than the range of its positive number range.

1.5 complement keywords

Here I have summarized several keywords about code population for your convenience.Negative integer,Machine font,Carry loss.

Negative integer: the original complement three codes of the positive integer are the same.

Machine font length: the completion code is closely related to the specific machine font length.

Carry loss: the most important thing is to understand the subtlety of carry loss.

Conclusion:

In this study, I learned the representation of integers in the memory, focused on the representation of negative integers in the memory, and the concepts related to the complement of negative integers.

In the next study, we will learn the representation of floating point numbers in the memory.

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.