# Understanding the number of symbols and the number of unsigned characters

Source: Internet
Author: User

DeclareArticleThe original text cannot be found, the original text cannot be reproduced, and there are many duplicates. It took a long time to sort it out. After reading Wikipedia, I found that there are some problems with negative original codes and supplementary codes in the original text, I modified some of them. The original author can contact me after seeing them.

1. You decide whether or not there must be positive or negative.

Just as we must determine whether an integer or a real number is used to determine the number of ranges, we must determine whether a certain number must be positive or negative. If this quantity does not have a negative value, we can set it to a positive or negative type.

In a computer, positive and negative types can be distinguished, which are called signed and positive-negative types (only positive values. (Unsigned) the numeric type can be an integer or a real type. The integer type can be a non-letter type or a letter type, while the real type can only be a character type. Character types can also be divided into break type and break type. For example, if there are two quantities, age and stock, we can set the former as the character type without a character, and the latter as the integer type with a character.

2. Use the highest digit in the binary number to indicate positive and negative.

First, you must know which one is the highest? 1-byte type, such as character type. The maximum value is 7th bits, the maximum value is 15th bits, and the maximum value is 31st bits. The highest bit of a value of different lengths is different, but it is always the leftmost (as shown below ). The character type is fixed to 1 byte, so the maximum bit is always 7th characters.

(Red indicates the highest digit)

Number of word segments:11111111

Double Bytes:11111111 11111111

Four bytes:11111111 11111111 11111111 11111111

When we specify a number to be of the unsigned type, the highest bit of 1 or 0 is used to indicate the size of the number like other bits.

When we specify a number to be of the unsigned type, the maximum number is called a "symbol bit ".If the value is 1, it indicates that this number is a negative value. If the value is 0, it indicates a positive value..

3. the unsigned number and the number range are different.

In the unsigned number, all bits are used to directly represent the size of the value. The highest bit in a signed number is used to indicate positive and negative values. Therefore, when it is a positive value, the maximum value of this number decreases. Let's take a byte value comparison:

Unsigned quantity: 11111111 value: 255

1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

Number of symbols: 01111111 value: 127

1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

It is also a byte. the maximum value of the unsigned number is 255, and the maximum value of the signed number is 127. The reason is that the highest bit in the number of symbols is removed to represent the symbol. In addition, we know that the highest bit of the weight is also the highest (for the number of 1 byte is 2 to the power of 7 = 128), so only less than one bit, the maximum value is halved at once.

However, the advantage of a signed number is that it can represent a negative number. Therefore, although it has shrunk in the maximum value, it is stretched in the negative direction. We still compare the values of one byte:

Unsigned Number:
0 ----------------- 255

Number of symbols:
-128 --------- 0 ---------- 127

It is also a byte. the minimum value of the unsigned value is 0, and the minimum value of the number of signed values is-128. Therefore, the numbers of different values can be the same as 256. However, the former expresses the numbers 0 to 255, and the latter expresses the numbers-256 to + 128.

How is the minimum value of a signed data type calculated?

The calculation method for the maximum value of the signed data type is exactly the same as that of the unsigned data type, except that the maximum bit is missing (see 3rd points ). However, within the negative value range, the calculation method of the value cannot be directly converted using the Formula 1*26 + 1*25. In a computer, a negative number is expressed in the form of a supplementary code in addition to the highest bit of 1. Therefore, you need to restore the complement code before calculating its value.
Here, you can take a look at the form of complement code:

In the decimal system, 1 indicates positive 1, and-1 indicates a negative value relative to 1.

Then, we can easily think that in binary (1 byte): 0000 0001 indicates positive 1, then after the High is 1: 1000 0001 should indicate-1.

However, in fact, the computer rules are somewhat different. See the following table:

 Binary value (1 byte) Decimal Value 10000000 -128 10000001 -127 10000010 -126 10000011 -125 ...... ...... 11111110 -2 11111111 -1

First, we can see that from-1 to-128, the highest bit of its binary is 1, as we learned earlier. The maximum negative value is 1.

Then we found that 1000 0000 was not used to represent-0, and 1000 0001 was not used to visually represent-1. In fact,-1 is represented by 1111 1111.

How can this problem be solved? First, you have to ask whether the value is-1 or-128?

Of course it is-1. -1 is the largest negative integer. In the computer, whether it is a character type or an integer type, or whether the integer is several bytes. It uses all 1 to represent-1. For example, if 1111 1111 represents-1 in a byte value, what is 1111 1111-1? It is exactly the same as the calculation result in reality. 1111 1111-1 = 1111 1110, and 1111 1110 is-2. In this way, the minimum negative value is 1000 in one byte, when only the highest bit is used to indicate the 1 of the symbol, and all the other low values are 0, that is,-128.

Let's take-1 as an example to see how to express-1 in Integers of different bytes:

 Number of bytes binary value decimal value Number of Single-word segments 11111111 -1 double bytes 11111111 11111111 -1 four bytes 11111111 11111111 11111111 -1

Some people may be confused at this time: Why does 1111 1111 sometimes indicate 255 and sometimes-1? So I will emphasize the 2nd point mentioned above: You decide whether a number is signed or unsigned. WriteProgramWhen a specified amount is signed, when the binary value of this amount is 1, it indicates-1; on the contrary, if you declare the value as unsigned, it indicates the maximum value allowed by the value. For the number of bytes, the maximum value is 255.

We already know that all data in a computer is expressed in binary. I have also learned how to convert a 10-digit number to a binary number. However, we still haven't learned how to express a negative number in binary.
For example, suppose there is a number of int type, the value is 5, then we know that it is represented in the computer:

00000000 00000000 00000000 00000101

5 is converted to a binary system of 101, but the number of int types occupies 4 bytes (32 bits), so a bunch of 0 values are filled in front. Now I want to know how-5 is represented in a computer? In a computer, a negative number is expressed in the form of a positive complement.

What is a supplemental code? This should start with the original code and the reverse code.

Original code: an integer that is converted to a binary number based on the absolute value. The highest value is the symbol bit, which is called the original code. Red indicates the symbol bit.

For example00000000 00000000 00000000 00000101 is the source code of 5.
10000000 00000000 00000000 00000101 is-5 original code

Reverse code: the bitwise inversion of the binary number of digits except the symbol. The new binary number obtained is called the reverse code of the original binary number. Positive anticode is the original code, and negative anticode is the bitwise opposite of the external symbol of the original code.
The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)

Positive: the inverse code of a positive number is the same as the original code.

Negative number: the inverse code of a negative number. The symbol bit is "1", and the value part is reversed by bit.

For example, if every bit of the 10000000 00000000 00000000 00000101 division symbol is reversed,

11111111 11111111 11111111 11111010.

Said: 11111111 11111111 11111111 11111010 10000000 is an anti-code of 00000000 00000000 00000101.

The anti-code is mutual, so it can also be called:

11111111, 11111111, 11111111, 11111010, 10000000, 00000000, 00000000, 00000101, and are mutually inverse codes.

Complement: the anti-code plus 1 is called a complement.
Positive: the positive complement is the same as the original code.

Negative number: according to the rule
That is to say, to get a complement of a number, first obtain the reverse code, and then add 1 to the reverse code. The resulting number is called a complement.

11111111 11111111 11111111 11111010 is the back code of 10000000 00000000 00000000 00000101 (-5.
Add 1 to 11111111 11111111 11111111 11111011

Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xfffffffb.

Let's look at how integer-1 is represented in a computer.

Suppose this is also an int type, then:

1. First retrieve the original code of-1: 10000000 00000000 00000000

2. Get the reverse Code except the symbol bit: 11111111 11111111 11111111 11111110

3. Add a Supplementary Code: 11111111 11111111 11111111 11111111

It can be seen that the Binary Expression of-1 in a computer is full 1. Hexadecimal: 0 xffffff.

Advantages of using a complement to represent the number of symbols in a computer:

1. The conversion between a negative complement and a positive complement can be completed using the same method-the completion operation can simplify the hardware;

2. subtraction can be changed to addition, eliminating the need for subtraction;

3. addition operations of the unsigned number and the signed number can be completed using the same circuit.

We can find a method for calculating and completing the mental computing. The value of the first 1 from the inner bit to the first 1 is not changed, and the sign bit remains unchanged. This method is only used to solve the problem ).

 method Example 1 Example 2 1. from the right, locate the first '1' 1010100 1 10101 1 00 2. Reverse all bits starting from this '1' to the leftmost (excluding the symbol bit) 1 101011 1 1 1010 100

Related Keywords:

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.