Why does the char number in the computer represent-128----127?

Source: Internet
Author: User
Tags ranges
Why is the range of 8-Bit Signed numbers "-128-+ 127 "? (Reposted and supplemented)

11:22:57 | category:

It | Tag:
| Large font size, medium/small subscription

This is a problem that has puzzled me for several years. It has left me extremely dissatisfied with the current textbooks and teachers. Since I started to touch my computer N years ago, in almost every C ++ textbook, the 8-Bit Signed value range is-128 ~ + 127, why not-127 ~ + 127. Later Java and INT values will be computed in 32 bits.-2 ^ 31
~ + 2 ^ 31-1, but no textbook or teacher has ever explained this question before. The reason is not met at work or somewhere, so I keep ignoring it, but there is always a thorn in my heart !!!!

Just now, I was bored. I met it again when I was reading the compiled book. But as before, the book directly and intentionally bypasses this problem. It's really awful.

After several twists and turns, I finally figured it out:

In fact, it is determined by the bottom layer of the computer to realize numerical computation. It involves a very basic source code, anti-code, and complement knowledge. Generally (99.9999%) it will not be used. that's 0.0001%, probably the computer test.

Statement:
If we use 2 ^ 8 to represent an unsigned integer, the world's understanding is 0-255. So what about the signed? Use the highest bit to represent the symbol. If 0 is + and 1 is-, the normal understanding is-127 to + 127.

This is the original code. It is worth mentioning that the weakness of the original code is that there are 2 0, that is, + 0 and-0, and when the difference sign is added or the same number is subtracted, dumb: first determine the absolute value of two numbers, and then perform addition and subtraction. Finally, the symbol of the calculation result must be the same as that of a large number.

As a result, the reverse code is generated. The cause is... slightly. Anyway, it didn't take long before the reverse Code became a filter product, that is, the complement code appeared.

I will not describe the supplemental knowledge, but only about + 127 and-128.

In the official definition [-2 ^ (n-1), 2 (n-1)-1], there is no positive or negative score for the 0 complement code. Why? No book says that this is why I am so angry, but through thinking, Google, and thinking, I quickly find the answer:

First of all, it's hard to do anything as an idiot. Just give it a try...
Positive number. The original code is the same as the complement code.
+ 127,011 1 1111
+ 126,011 1 1110
+ 125,011 1 1101
+ 124,011 1 1100
+ 123,011 1 1011
+ 122,011 1 1010
...
+ 4, 0000 0100
+ 3, 0000 0011
+ 2, 0000 0010
+ 1, 0000 0001
0, 0000 0000 (no positive or negative)

Below is a negative number, value, original code, the symbol bit remains unchanged, and the other is reversed, + 1

-1, 1000 0001,111 1 1110,111 1111
-2, 1000 0010,111 1 1101,111 1 1110
-3, 1000 0011,111 1 1100,111 1101
-4, 1000 0100,111 1 1011,111 1 1100
-5, 1000 0101,111 1 1010,111 1 1011
-6, 1000 0110,111 1 1001,111 1 1010
-7, 1000 0111,111 1 1000,111 1 1001
-8, 1000 1000,111 1 0111,111 1 1000
-9, 1000 1001,111 1 0110,111 1 0111
-10,100 0 1010,111 1 0101,111 0110
-11,100 0 1011,111 1 0100,111 0101
-12,100 0 1100,111 1 0011,111 0100
-13,100 0 1101,111 1 0010,111 0011
-14,100 0 1110,111 1 0001,111 0010
-15,100 0 1111,111 1 0000,111 0001
-16,100 1 0000,111 0 1111,111 1 0000
-17,100 1 0001,111 0 1110,111 0 1111
...
-24,100 1 1000,111 0 0111,111 0 1000
...
-99,111 0 0011,100 1 1100,111 0 0100
...
-124,111 1 1100,100 0 0011,111 1 1101
-125,111 1 1101,100 0 0010,100 0 0011
-126,111 1 1110,100 0 0001,100 0 0010
-127,111 1 1111,100 0 0000,100 0 0001
What do you see?
If not, give a prompt and continue. What is the next complement?

Of course yes
-128, skipped first, then skipped, 1000 0000

1000 0000, so what is the original code of it?
The method for obtaining the source code from the source code is the same as that for the source code.
First, retain the symbol bit. Other reverse expressions: 1111 1111, plus 0000, exceeding 8 digits.
Yes, it cannot be expressed here with an 8-digit original code.
The key is here. There is no need to suspect that the Supplementary Code 1000 0000 is-128 (the above example is poor ),
Then, return to the original code, and its original code is also 1000 0000 (the excess is automatically lost ),
What does 1000 0000 indicate in the original code? -0, but the complement Code specifies that 0 has no positive or negative points.
Let's take a look at how computation is performed in the computer:
For negative numbers, first take the absolute value, then reverse, and add one
-128-> 128-> 1000 0000-> 0111 1111-> 1000 0000
It's clear now.

Therefore, the complement expression of the 8-bit signed integer value range

1000 0000 to 0000 0000, and then to 0111 1111
That is,-128 to 0, and then to 127
End-128 ~ + 127

Thanks to Google, and to all the blogs that have been browsed, closed, and forgotten their names, csdn (there are some nice things above)

-------------------------------------------------------------------

Additional instructions

"An N-Bit Signed int value in the range of-2 ^ (n-1) -- 2 ^ (n-1)-1", many people are confused about this problem. In fact, the root cause of this situation is that "when people solve problems, they are used to thinking with people, but computers themselves are actually dealing with machine thinking ". Here, it is represented as "the computer's data processing is actually in the form of 'captcha CAPTCHA ', rather than the 'original Code' format used by people in daily life for mathematical operations ", however, when people are dealing with the "value range", they habitually adopt the "original code as the machine code ".

In history, the computer has adopted three types of machine codes, namely the original code, reverse code, and complement code, for "value" computation.

The statement is as follows:

1. The highest bit of the original code is the symbol bit, and the rest are the binary values of the absolute values of the corresponding values;

2. The highest bitwise of the anti-code is the symbol bit, and the positive value = negative bitwise of the original code = symbol bit + other digits corresponding to the original code are reversed;

3. The highest bit of the complement code is the sign bit, and the positive number = negative number of the original code = reverse code + 1.

The symbol "0 is +, and 1 is -".

Because the computer assigns N bits to the data type and the N-bit value is automatically discarded, you can find that the complement code used in the computer system is, overcome the situation that "the original code contains + 0 and-0", and only represents 0 (for 8-bit data types, that is, 8 0, for details about the derivation, refer to the reference in the reprinted content.-128: 11000 of the 9-bit data generated when the code is calculated-0000-1000 of the Code ).

Correspondingly, the range of 8-Bit Signed values becomes "-2 ^ (8-1) -- 2 ^ (8-1)-1 ", that is, "-128 -- + 127 ".

The key to the problem is here, "the computer allocates a fixed number of digits N for the signed int value to store data. When the number of digits is greater than N, the number of digits greater than N is automatically discarded ". This is the reason that the value range is "-2 ^ (n-1) -- 2 ^ (n-1)-1". This also leads to the generation of the "modulo" concept in the data range.

Note:

1. Module

The "module" is essentially the amount of overflow produced by the metering device. Its value cannot be expressed on the metering device, and the metering device can only represent the remainder of the output model. For example, although the clock mode is 12, the pointer to the clock cannot really point to "12 o'clock", and the position of "12 o'clock" is coincident with "0 o'clock, that is, 12 points are expressed as 0 points.

In other words, if the clock range is "0-11", the modulo is 12.

Correspondingly, the N-bit unsigned value ranges from 0 ~ 2 ^ (N)-1, modulo = 2 ^ (n); N-Bit Signed value, value range-2 ^ (n-1) -- 2 ^ (n-1)-1, then the modulo is 2 ^ (n-1 ). For example, an 8-bit unsigned value. The binary modulo is 2 ^ 8. The 8-Bit Signed value ranges from 0 to 2 ^ 8-1.

2. complement calculation rules

[X + y] fill = [x] fill + [y] fill; [X-Y] fill = [x] fill-[y] fill = [x] fill + [-y] fill.

 

Hope to help you!

 

Refer:


Http://baike.baidu.com/view/377340.html? WTP = TT


Http://baike.baidu.com/view/742694.htm? Fr = ala0_1


Http://baike.baidu.com/view/60480.htm

Http://www.cppblog.com/goal00001111/archive/2010/04/16/112745.htmlAnd so on

Related Article

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.