Code mystery (1)-limited and infinite (starting from the absolute value of an integer)

Source: Internet
Author: User
I. Introduction

Before starting this chapter, I would like to raise a question: "If the absolute value of an integer is equal to itself, What is the number ?" If your answer is 0 and all positive numbers, please read this article with patience.

This chapter is the second part of my "code Mystery" series. The previous article "code mystery-opening/preface" briefly introduces the differences between computers and mathematics.

There are many complicated and profound contradictions in mathematics. The mathematician's job is to explain or refute these contradictions, such as finite and infinite, continuous and discrete, existence and construction, logic and intuition, specifics and abstraction, concepts and computing, etc.

In this chapter, we will narrow down the objectives and mainly discuss the content.

  • Concept: Limited and unlimited
  • Object: 8-bit integer
II. The mystery of absolute values

Finally, the topic is reached. Maybe you want to know that "is the absolute value of a negative number equal to yourself ?", That is, "If X is equal to-X, how many solutions does X have ?" According to my consistent style, I will not tell you the answer easily. The author tells his colleagues a result, rather than a method, and finally regrets it. So here I wantTell you the method instead of the answer.

Before giving you an answer, you must first answer the question: "What is the meaning range of an integer (8 bit ?", (Maybe you have backed up the textbook knowledge, which is-2 ^ 7 to 2 ^ 7-1, that is,-128 to + 127, now computer science is becoming a liberal arts ).

It doesn't matter if you don't know. At least you know that 8 bits can represent 2 ^ 8 integers. It doesn't matter how many bits are equal, but it must be an even number (256 ).

Here is a very interesting question. 0 is neither a positive nor a negative number. If we remove 0, the number of integers is an odd number, and there are 255 integers left. An odd integer cannot be divided into two parts (positive number and negative number) in average, either one more negative number or one more positive number. The fact is that there is one more negative number than the positive number, the smallest negative number is-128, and the maximum integer is 127.

The question is, what is the absolute value of-128? -(-128) is equal to what? Is it overflow, or is it equal to itself? Maybe the computer textbook didn't tell you,Integers do not cause overflow exceptions.Integer Overflow is considered a normal discard (as long as it is reasonable ).An integer is abnormal only when it is divided by 0, and a floating point number does not throw an exception even if it is divided by 0.. The operations for removing floating point numbers from 0 will be discussed in this series of floating point numbers.

The absolute value is equal to the number of its own. There are two numbers, 0 and the smallest negative number.

You may want to scream like the heroine in a Hong Kong movie, "how is the absolute value negative? No, I don't believe it, I don't believe it ..."

Forget your poor math knowledge. "This kind of thing doesn't happen to everyone. Emotional matters cannot be forced. The so-called auspicious people have their own days, people, the most important thing is happy ..." Run the question and hurry back.

In classical mathematics (a non-Piano arithmetic system, piano is definitely a hardcore fan of Euclidean, or how can he have such a concept of genius, which will be popularized in the future), the absolute value is defined: "The distance from the origin point to a is called the absolute value of A. It is a non-negative number ". Now that we have talked about the distance, let's take a look at it (this series of "logical articles" will involve). How can we define the size of two numbers in mathematics: "distance from the number axis origin ", how do we define big and small computers? Leave a homework for everyone (do not tell me that computer science is definitely not a liberal arts program, as defined by scientists who design programming languages or computers ).

The computer does not have a number axis. How is the absolute value defined? Let's look at the source code of Java, C, and Python (thanks to those open-source experts), just like the Primary School Mathematics we have learned.

abs(x) :=  (x >= 0) ? x : -x

The absolute value of X is defined as: the absolute value of positive and 0 is equal to itself, and the absolute value of negative is equal to-X. (Here we use-X instead of 0-x, because the two are different in floating point numbers .)

Iii. In-depth-x

So how is-x calculated?ComputingIt is a mathematical concept. in computer science, we should say-xEvaluate? I have to go back to the source code. I only read the C source code in Linux. If you have read the source code of other languages and find that it is different from what I said, please contact me. All those who have learned about computer principles know that negative numbers are stored in the computer in the form of complement code. The method for calculating the complement code is similar to that for obtaining the reverse code.

The symbol bit remains unchanged. The other bits are reversed, and the last one is added.

For example,-5

Original code: 1000,0101 other bitwise reverse: 1111,10plus: 1111,1011

What should we do when we find its absolute value?

Complement: 1111,1011 this is-5 in the computer, it indicates that you take the reverse: Random, 0100 plus one: Random, 0101 at this time the result is + 5

Now we return to the minimum negative number problem. The minimum negative number is expressed as 1000,000 in the computer. Next we operate on this number.

Complement:, reversed: 0111,1111 plus one :,

What a magic! Nima, I actually got back to myself.

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.