# Reprinted: binary encoding-negative number

Source: Internet
Author: User

Negative Number:

The original code is the original representation method.

The reverse code is used to retrieve the reverse Code except the sign bit (highest bit ).

Complement = reverse code + 1

When I used to learn binary encoding, the teacher talked about a pile of original codes, reverse codes, supplementary codes, xxxx conversions, and negative expression methods, finally, I found a better way to explain it on the Internet. I saved it and shared it. However, I found some basic coding knowledge to systematically explain it. If I only want to see the negative number encoding memory method, please jump

1. If you do not know how to encode binary data, continue; otherwise, skip to 2.

1 byte = 8 bits, so the maximum number it can represent is of course 8 bits are all 1 (since the number of binary can only be 0 or 1, if it is our common 10-digit system, the 8-digit system will all be 9. In this case, you should understand it ?)

The maximum number of 1-byte binary: 11111111.

What is the size of this number? Let's convert it to a decimal number.

No matter what the hexadecimal format is, the left is the high level, and the right is the low level. We are very accustomed to using the 10-digit counting method. The first one represents a few 1 (that is, a few 100), and the second one represents a few 10 (that is, a few 101 ), the third digit represents several hundred thousand (that is, several hundred thousand )..., In the elementary school textbooks, the number in a single place represents 1, the number in 10 represents 10, and the number in a hundred represents 100 ......

Likewise, the binary number is: the number of digits 1st represents a few 1 (20), the number of digits 2nd represents a few 2 (21), and the number of digits 3rd represents a few 4 (22 ), 4th bits indicate 8 (23 )......

We used to know that one byte has eight bits. Now, through computation, we know that the maximum number of 1 byte expressions is 255, that is, 0 ~ 255: the number of 256.

So what about two bytes (two bytes? 16 bytes in total. 1111111111111111, this number is not big, but it looks a little dizzy. From now on, we should learn to express the binary number as follows:

1111 1111 1111 1111, that is, every four spaces.

The maximum number of double bytes is:

1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + ...... + 1*22 + 1*21 + 1*20 = 65535

Naturally, we can think that the maximum allowed value of a data type is related to its number of digits. The specific calculation method is: if it has N bits, the maximum value is:

Maximum N-bit binary: 1*2 (n-1) + 1*2 (n-2) +... + 1*20

2. Understand the number of signed and unsigned characters

How do negative numbers be expressed in the computer? You may have heard two different answers.

One is a textbook, which tells you that a computer uses a "complement" to indicate a negative number. However, the concept of "complement code" is a lesson. We need to use a chapter in Chapter 2 to talk about everything in binary notation. Furthermore, a negative number is expressed by a "complement". The formula is used to tell you how to calculate the answer to the question. But I didn't tell you why I could use this formula to answer the question? ----- I am confused by this >_<

The other isProgramThe highest bit of the binary number represents the symbol. The highest bit is 0, indicating a positive number, and the highest bit is 1, indicating a negative number. This statement is true, but if it is not described below, it is wrong. At least it cannot explain why-1 of the character type is expressed as "1111 1111" in binary format (with a hexadecimal value of ff), rather than "1000 0001" that we can better understand ". (Why is the latter better understood? Since when the highest bit is 1, it indicates a negative number, isn't 1000 0001 exactly-1? ----- Re! This is what I thought at the beginning. So has been fighting in the mind, and the more confused it is. =, = ).

Let's start from scratch.

2.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 the operator type and the positive and negative types (only positive values.

The numeric type can be an integer or a real type. The integer type can be a non-letter type or a character 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 single words: 1111 1111

Double Bytes: 1111 1111 1111 1111

Four bytes: 1111 1111 1111 1111 1111 1111 1111 1111

When we specify a quantity that isUnsigned typeThe value of 1 or 0 is the same as that of other bits.

When we specify a quantity that isSigned typeAt this time, the maximum number is called "symbol bit ". If the value is 1, the number is negative. If the value is 0, the value is positive.

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: 1111 1111 value: 255 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

Symbol Number: 0111 1111 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

Signed quantity:-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 our original mathematical experience, in the 10th hexadecimal notation: 1 represents positive 1, and with a negative number:-1 represents 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 1 000 0000 the Red 1 represents a negative number, and the blue is a complementary code (complement = reverse code + 1) -128 1 000 0001 what is the value of the blue part?: Restore the complement code to the original code -127 convert to negative number?: minus 1 and then bitwise inversion 1 000 0010 restoration method: complement-1 and reverse retrieval -126 1 000 0011 -125 ... ... 1111 1110 -2 1 111 1111 -1

First, we can see that from-1 to-128, the highest binary bits are 1 (the table is marked in red), as we learned earlier.

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.

-------- Annotation: This is the blue text, so that I can finally remember the encoding method of-1, Khan =. =

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

 Bytes Binary Value Decimal Value Number of Single-word segments 1111 1111 red indicates that the complement of the negative blue part is 1 -1 Negative number: the original code is the original representation method. The reverse code is the reverse Code except the symbol bit (highest bit) and the complement code = the reverse code + 1 double byte. 1111 1111 1111 1111 -1 Four bytes 1111 1111 1111 1111 1111 1111 1111 -1

Some people may be confused at this time: Why does 1111 1111 sometimes indicate 255 and sometimes-1? So I want to emphasize the 2nd point mentioned above: You decide whether a number is signed or unsigned. When writing a program, specify a volume that is signed. When the binary value of this volume 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.

Okay, I have excerpted the interim section. In fact, some basic data type knowledge about C is very detailed in the original article, which is too long. After I have picked the content I need, I didn't have a full post, if you need to learn, we recommend that you refer to the original article :)

From http://blog.cersp.com/7892477/1201309.aspx

Keywords: binary encoding, negative binary, binary

1-6 What is machine count? Why do computers use supplementary codes? 14:24:25 Big, medium, and small labels: educational discussion

In a computer, all information is represented in binary strings. Integers generally have positive and negative values. integers in a computer are unsigned and signed. An unsigned integer is used to represent 0 and a positive integer. A signed certificate can represent all integers. Because the symbols in the computer are the same as numbers, they must both be represented by binary numbers. Therefore, positive and negative numbers must also be represented by 0 and 1. Generally, we use the highest valid bits to represent the number. (when 8 bits are used to represent an integer, 8th bits are the highest valid bits. When 16 bits are used to represent an integer, 16th bits indicates the highest valid bits .) 0 indicates the positive number and 1 indicates the negative number. The digital representation of the positive and negative numbers is called the number of machines. The external representation of the corresponding machine is called the "true value ". The process of displaying a truth table as a machine number of binary strings is called encoding.

The unsigned number does not contain the original code, reverse code, or complement code. Different encoding methods are available only for the number of bits.

There are several encoding methods for signed integers, such as source code, reverse code, and complement code. The original Code directly converts the true value to the corresponding binary form, and the reverse code and the complement code are some conversion encoding methods for the original code. The original, reverse, and complement codes of positive integers are the same, the negative number is the result of the inverse operation on the other bits except the sign bit. (If this bit is 0, it is changed to 1, if this bit is 1, it is 0 ). The complement operation is the result of first obtaining the back code of the original code, and then adding 1 at the end of the back code, that is, the complement operation is the back code + 1. Signed integers in the IBM-PC are represented in the form of complement. (Note: Only signed integers are stored in the bucket, and floating point numbers are stored in the bucket .)

The reasons or benefits of using the supplemental Code are as follows.

The complement operation has the following two features:

1) because the complement code can be used to process the symbol bit and other bits in a unified manner, the subtraction can also be processed by addition, that is, if it is the number indicated by the complement code, addition and subtraction can be implemented directly by addition and subtraction.

2) When two numbers are added, if the highest bit (symbol bit) has an incoming bit, the carry is discarded.

1) enable the symbol bit to join the operation with the valid value to simplify the operation rules. This simplifies the structure of the generator and increases the computing speed. (subtraction can be expressed by addition .)

2) addition operations are easier to implement than subtraction operations. Converts the subtraction operation to addition, further simplifying the line design of the calculator in the computer.

The following is an in-depth analysis of the reasons for using the Supplementary Code (objective) stated above ).

The result of the multiplication and division operation with the original code with the symbol bit is correct, and the addition and subtraction operation has a problem, as shown below: Assume the length is 8 bits

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) ORIGINAL + (10000001) original = (10000010) original = (-2) is obviously incorrect ..

Because there is no problem in the addition operation of two integers, the problem occurs on a negative number with a symbol bit, A reverse code is generated for all the characters except the symbol bit. The anticode value space is the same and one-to-one correspondence with the original code. The following is the subtraction operation of the anticode:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) there is a problem with anti + (11111110) anti = (11111111) anti = (-0.

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) + (11111101) = (11111110) = (-1) Correct

The problem occurs in (+ 0) and (-0). In people's computing concepts, Zero is not divided into positive and negative.

So the concept of Code complement is introduced. The complement of a negative number is to add one to the anticode, while the positive number remains the same. The original code of the positive number is the same. (-0) is replaced by (-128) in the complement code, so the range of the complement code is:

(-128 ~ 0 ~ 127) a total of 256.

Note: (-128) there is no corresponding source code and anticode. The addition and subtraction operations of (-128) = (10000000) are as follows:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) Fill + (11111111) Fill = (00000000) Fill = (0) Correct

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) Fill + (11111110) Fill = (11111111) Fill = (-1) Correct

Another reason is that the number of machines with 0 has two encodings. The original code and the reverse Code represent 0 in the form of + 0 and-0, and we know that + 0 and-0 are the same. In this way, the range of the 8-bit original code and the integer represented by the anticode is-127 ~ + 127 (11111111 ~ 01111111), and when the complement representation is used, 00000000 is + 0, that is, 0; 10000000 is no longer-0, but-128. In this way, the value range of the complement representation is-128 ~ + 127, not only adds a number representation range, but also ensures the uniqueness of the 0 encoding.

The source code, reverse code, and complement code of the integer and 0 are the same. The following describes how to manually calculate the complement code of the negative number. This method has been mentioned on the 8th page of the textbook. I will write it again here to attract your attention. The method is as follows:

First, write the opposite number (positive number) of the negative number, then write the binary form of the positive number, and then take the inverse of the binary string by bit, that is, if 1 is changed to 0, if it is 0, change it to 1, and Add 1 to the last position.

The next question is, how can we convert a subtraction operation to an addition operation?

We already know that the original code is simple and intuitive, and it is easy to convert to the true value. However, if the original code is used, its symbol bit cannot be used in the operation. When the original code is used in a computer to perform arithmetic operations, the absolute value must be taken for calculation, and the symbol bit is processed separately. This is easy to implement for multiplication and division operations, but it is very inconvenient for addition and subtraction operations, for example, if two different numbers are added, subtraction is actually required, while addition is actually required for two different numbers. When performing subtraction, you also need to determine the absolute value of the operand, which will make the design of the iterator very complicated. This encoding method is actually aimed at the above problems. The subtraction operation can be converted into addition operations by using the complement code.

In daily life, there are many examples of subtraction and addition. For example, the clock runs at 12 bits, and can also be regarded as zero. There are two ways to adjust the hour hand from 10 to 5:

One way is to dial 5 cells in the clockwise direction, which is equivalent to subtraction:

10-5 = 5

Another method is to dial 7 cells clockwise, which is equivalent to addition:

10 + 7 = 12 + 5 = 5 (mod 12)

This is because the clock uses 12 as the model. On this premise, when the sum of 12 or more times, 12 can be discarded. Therefore, subtraction of 5 is equivalent to addition of 7. Similarly, subtraction 4 can be used to add 8, subtraction 3 can be used to add 9 ,....

In mathematics, we use the concept of "same remainder" to describe the above relationship, that is, two integers.A,BUse the same positive integerM(MThe remainder is equalA, BPairMSame remainder, recorded:

A=B(ModM)

There is a complementary relationship between the two numbers that have the same remainder, one of which is called the complement code of the other. When M = 12,-5 and + 7,-4 and + 8,-3 and + 9 are the same, and they complement each other.

From the concept of the same remainder and the example of the above clock, it is not difficult to draw a conclusion: for a certain modulo, use a certain number to subtract another number smaller than the modulo, the sum can be replaced by the difference between the modulus and the absolute value of the number. Therefore, in a modulo operation, subtraction can be converted into addition.

It can be seen that the basic relationship of the addition operation of the complement code is:

[X] fill + [y] fill = [x + y] fill

The basic relationship between the complement and subtraction methods is as follows:

[X-y] fill = [x + (-y)] fill = [x] fill + [-y] fill

As to why addition operations are easier to implement than subtraction operations and how the CPU implements various arithmetic operations, you need to learn about the hardware implementation of the CPU generator by learning the digital circuit.

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.