Why is the numeric type byte value range ( -128~127)?

Source: Internet
Author: User

Before we solve this problem, we should understand a few concepts.

First, the concept of the original code, the inverse code, the complement

  

The inverse code and the complement of positive numbers are the same as the original code;

Negative inverse code, complement and the original code is different, negative anti-code: The original code to remove the sign bit, the other value of the reverse, 0 change to 0. The complement of negative number: its inverse code +1.

Do a small demo, respectively write 7 and 7 of the original code, anti-code, and complement. (where the first bit is the sign bit, 0 is a positive number, and 1 indicates a negative number)

 

Demo

7

-7

Original code

00000111

10000111

Anti-code

00000111

11111000

Complement

00000111

11111001

a . machine number and truth value

Before you learn the original code, the inverse code and the complement, you need to understand the concept of machine number and truth value first.

1 , number of machines

A binary representation of a number in a computer, called the number of machines. The number of machines is signed, in the computer with a number of the highest bit of the symbol, positive 0, negative number is 1.

For example, in decimal number +3, the computer word length is 8 bits, converted to binary is 00000011. If it is-3, it is 10000011. So, here's 00000011 and 10000011 is the number of machines.

2 , Truth

Because the first bit is the sign bit, the form value of the machine number is not equal to the true value. For example, the above signed number 10000011, its highest bit 1 is negative, its true value is-3 instead of the form value 131 (10000011 is converted to decimal equals 131). So, for the sake of differentiation, the true value of the number of machines with the sign bit is called the truth of the machine number.

Example: 0000 0001 Truth value = +000 0001 = +1,1000 0001 True Value =–000 0001 =–1

two . Original Code , Anti-code , the basic concept and calculation method of complement .

Before we explore why machines need to be complementary, let's first understand the concepts of the original code, the inverse code, and the complement. For a number, the computer is stored with a certain encoding. The original code, anti-code, and the complement is the machine to store a specific number of encoding method.

1. Original Code

The original code is the absolute value of the symbolic bit plus the truth, that is, the first digit represents the symbol, and the remaining bits represent the values. For example, if it is a 8-bit binary:

[+1] original = 0000 0001

[-1] original = 1000 0001

The first bit is the sign bit. Because the first bit is the sign bit, the range of values for the 8-bit binary number is:

[1111 1111, 0111 1111]

i.e. [-127, 127]. The original code is the most easily understood and calculated representation of the human brain.

2. Anti-code

The inverse code is represented by:

The inverse of a positive number is its own

Negative number of the inverse code is on the basis of its original code, the sign bit is unchanged, the remaining bits are reversed.

[+1] = [00000001] original = [00000001] Reverse

[-1] = [10000001] original = [11111110] Reverse

It can be seen that if an inverse code represents a negative number, the human brain cannot visually see its value. It is usually converted to the original code and then calculated.

3. complement

The complement is expressed in the following ways:

A positive complement is its own

The complement of a negative number is on the basis of its original code, the sign bit is unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1)

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

[-1] = [10000001] original = [11111110] counter = [11111111] Complement

For negative numbers, the complement representation is also the human brain can not intuitively see its value. It is also often necessary to convert the original code to calculate its value.

three . Why to use the original code , Anti-code and complement

Before I began to study, my study advice was to first "memorize" the original code, anti-code and complement of the expression and calculation method.

Now we know that a computer can have three encoding methods to represent a number. For positive numbers, the results are the same for three encoding methods:

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

So there's no need to explain too much. But for negative numbers:

[-1] = [10000001] original = [11111110] counter = [11111111] Complement

Visible source code, anti-code and complement is completely different. Since the original code is directly recognized by the human brain and used to calculate the representation, why there are anti-code and complement it?

First of all, because the human brain can know that the first bit is the sign bit, in the calculation we will select the value of the truth region according to the sign bit. (The concept of truth is at the beginning of this article). However, for computers, the multiplier is the most basic operation, to be designed as simple as possible. Computer identification of "sign bit" will obviously make the computer's basic circuit design become very complex! So people came up with a way to take symbolic bits into the operation. We know that by subtracting a positive number from the algorithm equals adding a negative number, namely: 1-1 = 1 + (-1) = 0, so the machine can only add without subtraction, so the design of the computer operation is simpler.

So people began to explore the use of symbolic bits to participate in operations, and only preserve the method of addition. First look at the original code:

Expression for calculating decimal: 1-1=0

1-1 = 1 + (-1) = [00000001] Original + [10000001] original = [10000010] Original = 2

If the symbol is represented in the original code, it is obvious that the result is incorrect for subtraction. This is why the computer does not use the original code to represent a number.

In order to solve the problem of the original code subtraction, there is an inverse code:

Expression for calculating decimal: 1-1=0

1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original = [0000 0001] Anti + [1111 1110] anti = [1111 1111] reverse = [1000 0000] Original = 0

It is found that the true value of the result is correct by inverse code calculation and subtraction. The only problem is in the "0" of this particular value. Although it is understood that +0 and 0 are the same, the 0 notation does not make any sense. And there will be [0000 0000] Original and [1000 0000] The original two codes represent 0.

So the complement appeared, solved 0 symbols and two coding problems:

1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original =[0000 0001] Anti + [1111 1110] anti = [0000 0001] Complement + [1111 1111] complement = [0000 0000] Complement =[000 0 0000] Original

So 0 is denoted by [0000 0000], and 0 of the previous problem does not exist. And you can use [1000 0000] to represent-128:

(-1) + (-127) = [1000 0001] Original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] Complement

The result of 1-127 should be-128, in the result of the complement operation, [1000 0000] The complement is-128. But note that because it is actually using the complement of the previous-0 to represent-128, so-128 does not have the original code and the inverse code representation. (The complement of 128 [1000 0000] complements the original code is [0000 0000], which is incorrect), using the complement, not only fixed the 0 symbol and the existence of two coding problems, but also can represent a minimum number. This is why the 8-bit binary, which uses the original code or the inverse code to represent a range of [-127, +127], and the use of the complement expressed in the range of [-128, 127].

Because the machine uses the complement, for the 32-bit int type that is commonly used in programming, you can indicate that the range is: [-231, 231-1] because the first bit represents the sign bit. You can save a minimum value when you use the complement notation.

Four, Original Code , Anti-code , complement Go deeper

The computer skillfully participates in the operation of the sign bit and turns the subtraction into addition, what is the mathematical principle behind it?

Think of a clock as a 1-bit 12-digit binary number. If the current time is 6 points, I would like to set the time to 4 points, how to do it? We can:

1. Dial back 2 hours: 6-2 = 4

2.10 hours Forward: (6 +) MoD 12 = 4

3. Forward 10+12=22 Hours: (6+22) mod 12 =4

The MoD in the 2,3 method refers to the modulo operation, and the remainder of MoD 12 =4 16 divided by 12 is 4.

So the result of clock back (subtraction) can be replaced by forward dialing (addition)!

The focus now falls on how to use a positive number instead of a negative number. In the example above we can feel some clues and find some rules. But maths is rigorous. Not by feeling.

First, a related concept in mathematics is introduced: congruence

Concept of congruence

Two integers a, b, if they are divided by the remainder of the integer m, it is said that a, b for modulo m congruence

Remember as A≡b (mod m)

Read A and b about modulo m congruence.

To illustrate:

4 MoD 12 = 4

MoD 12 = 4

MoD 12 = 4

So 4, 16, 28 about modulo 12 congruence.

Negative number modulo

A positive number for the MoD operation is simple. But what about negative numbers?

Here is a mathematical definition of the MoD operation:

Above is, "remove the bounds" symbol can not find how to input (pasted in Word after the garbled). The following are the "remove bounds" symbols replaced with "L" and "J":

X mod y = x-y L x/y J

The above formula means:

x mod y equals x minus y by the lower bound of the quotient of X and Y.

Take 3 mod 2 for example:

-3 MoD 2

= -3-2XL-3/2 J

= -3-2xl-1.5j

= -3-2x (-2)

=-3 + 4 = 1

So:

( -2) MoD 12 = 12-2=10

( -4) MoD 12 = 12-4 = 8

( -5) MoD 12 = 12-5 = 7

Start proving

Back to the question of the clock:

Callback 2 hours = 10 hours before dialing

Callback 4 hours = 8 hours before dialing

Callback 5 hours = 7 hours before dialing

Attention, the law found here!

Combined with the concept of congruence as learned above. In fact:

( -2) MoD 12 = 10

MoD 12 = 10

-2 and 10 are the same.

( -4) MoD 12 = 8

8 MoD 12 = 8

-4 and 8 are the same.

The distance is getting nearer and closer to success. To achieve a positive substitution of negative numbers, only two theorems of the same remainder are required:

Reflexivity:

A≡a (mod m)

This theorem is very obvious.

Linear operation theorem:

If A≡b (mod m), C≡d (mod m) Then:

(1) a±c≡b±d (mod m)

(2) A * c≡b * d (mod m)

If you want to see the proof of this theorem, see: http://baike.baidu.com/view/79282.htm

So:

7≡7 (mod 12)

(-2) ≡10 (mod 12)

7-2≡7 + ten (mod 12)

Now we have a negative number and find its positive number as the remainder. But not 7-2 = 7+10, but 7-2≡7 + (mod 12), that is, the remainder of the calculated result is equal.

Next go back to the question of binary, take a look: 2-1=1 problem.

2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Anti + [1111 1110] Inverse

First, 1 of the anti-code representation is 1111 1110. If [1111 1110] is considered to be the original code, then [1111 1110] The original =-126, where the sign bit is removed, which is considered to be 126.

The following rules have been found:

( -1) MoD 127 = 126

126 MoD 127 = 126

That

(-1) ≡126 (mod 127)

2-1≡2+126 (mod 127)

2-1 the remainder of the 2+126 is the same as the result! And this remainder, formally our expectation of calculation results: 2-1=1

So the inverse of a number is actually the same remainder of the number for a membrane. And this film is not our binary, but the maximum value that can be expressed! It's like a clock, and you can always find the right value in the range you're looking for in a round!

The 2+126 is obviously the equivalent of a clock turning round, and because the sign bit is involved in the calculation, just as the highest level of overflow to form the correct results.

Since the anti-code can be subtraction into addition, now the computer uses the complement it? Why add 1 on the basis of anti-code and get the correct result?

2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Complement + [1111 1111] Complement

If you take [1111 1111] as the original code, remove the sign bit, then:

[0111 1111] Original = 127

In fact, on the basis of anti-code +1, just equivalent to increase the value of the membrane:

( -1) MoD 128 = 127

127 MoD 128 = 127

2-1≡2+127 (mod 128)

At this point, the dial corresponds to one round of every 128 ticks. So the minimum and maximum values of the results of the operation in the complement should be [-128, 128].

But because of the special case of 0, there is no way to represent 128, so the range of complement is [-128, 127]

Declaration reproduced from Http://www.linuxidc.com/Linux/2015-02/113863.htm

Why is the numeric type byte value range ( -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.