Representation of negative numbers in a computer

Source: Internet
Author: User
Today, the boss asked me to investigate the problem of converting a floating-point number to an integer. Self-check some information, by the way to review the original code, anti-code and complement.   Original code: Converts an integer into binary, which is its original code. The original code for the single-byte 5 is: 0000 0101;-5 The original code is 1000 0101. Anti-code: the inverse of a positive number is its original code, negative number of the anti-code is the original code, in addition to the symbol bit, each bit reversed. such as the single-byte 5 of the Anti-code is: 0000 0101;-5 The original code is 1111 1010. Complement: A positive complement is its original code, negative anti-code +1 is the complement. If the complement of the single byte 5 is: 0000 0101;-5 The original code is 1111 1011.     In a computer, positive numbers are expressed directly in the original code, such as single-byte 5, which is represented in the computer as: 0000 0101. A negative number is a complement, such as a single-byte-5, represented in a computer as 1111 1011. There is a question here, why in the computer, negative numbers are expressed in the complement. Why not just use the original code to express. such as single byte -5:1000 0101. I want to consider the software for two reasons:   1, representing the range of single-byte integers, the unsigned type, which represents a range of [0,255], representing a total of 256 data. A signed type whose representation range is [-128,127]. Look at unsigned first, 0 means 0000 0000,255 is 1111 1111, just meet the requirements, can represent 256 data. Look at the symbol, if the original code, 0 is expressed as 0000 000. Because we have symbols, we should also have a minus 0 (although it is still 0): 1000 0000. So let's see if we can meet our requirements, say 256 data. Positive, no problem, 127 is 0111 1111,1 is 0000 0001, of course, the other should be no problem. Negative, 1 is 1000 0001, then the minus, the largest number is 111 1111, which is 127, so the smallest data in negative numbers is-127. This seems to be not quite right, how to express-128. Seemingly direct use of the original code can not be expressed, and we have two 0. If we designate one of the 0 as-128, No. This is also an idea, but there are two problems: one is that it is too large with the 127 span, and the other is that it is inconvenient to operate with hardware. Therefore, in the computer, negative numbers are expressed in complement. such as single-byte-1, the original code is 1000 0001, the anti-code is 1111 1110, the complement is 1111 1111, the computer's single-byte 1 is represented as 1111 1111. Single-byte-127, the original code is 1111 1111, anti-code 1000 0000, the complement is 1000 0001, the single-byte-127 representation of the computer is 1000 0001. Single-byte-128, the original code does not seem to show, in addition to the symbol, the maximum number can only be 127, its representation in the computer is 1000 0000.   2, the size of the habit (personal view) can also be understood from the size of the data. Take the example of single-byte data. In the signed number, the range of positive numbers is [1,127], the largest is 127, regardless of the symbol, which is expressed as 111 1111, the smallest is 1, regardless of the symbol, which is expressed as 000 0001. The largest of the negative numbers is-1, and we use 111 1111 to represent the value part. The subsequent data is reduced by 1 in turn. Minus 000 0001, we used it to mark-127. Minus 1, it becomes 000 0000. Fortunately we have a symbol for, so there are two 0. Take the signed 0 and show that-128, just to meet the presentation range.     above is only from the perspective of software analysis, of course, from the point of view of the hardware, negative use of complement to indicate that there is a reason, after all, the computer, the final implementation of computing or hardware. There are three main reasons:   1, the complement of negative numbers, and the corresponding positive complement of the conversion can be done by the same method----the completion of the calculation, simplifying the hardware. such as:                       Original Code                 anti-code                 Complement-127-〉127     0001 -〉0111 1110 -〉0111 1111 127-〉-127    0111 1111 -〉1000 0000&nbs P -〉1000 0001-128-〉128    0000 -〉0111 1111 -〉1000 0000 128-〉-128    0000 -〉0111 1111 -〉  1000 0000 It can be found that the method of solving negative and positive numbers is The same.   2, you can change the subtraction to addition, eliminating the subtraction device. In a computer, we can see that the result is a negative number corresponding to its value. The same is true for negative numbers. Arithmetic, minus a number, equals the opposite number of it, the primary school. Since its complement is the opposite number, we add its complement to it. such as: A-127, also is equivalent to: A + (-127), and because the negative number is in the form of a complement, that is, negative value is the complement of the truth, since so, when we want to reduce a number, direct its complement to take over, add a bit, OK, we can also rest assured to say goodbye to the subtraction. Of course, this also involves the type conversion problem, such as single byte 128, its original code is 1000 0000, its complement is also 1000 0000. So we are 128, or-128, are to take 1000 0000 to add, so that no confusion dropped. Fortunately, the editors of each programming language have restrictions related to the type conversion. such as: (assuming the constants are single-byte) 1 + 128, the true value operation is 0000 0001 + 1000 0000, if you assign the result to a single-byte signed positive number, the editor prompts you to go beyond the presentation range. Because the two data of the operation is unsigned, the result is unsigned 129, and the symbol single-byte variable can represent a maximum of 127. 1-128, the knowledge of the operation is 0000 0001 + 1000 0000, because 128 is signed, and its operation result is signed, 1000 0001, just is-127 in the computer truth.   3, unsigned and signed addition operations can be done with the same circuit. Both symbolic and unsigned additions are actually taken to add the truth. The truth value, which is a binary representation of a number in a computer. The true value of a positive number is its original code, and the true value of a negative number is its complement. Therefore, signed and unsigned by the compiler control, the computer to do the mend is to take two truth to add.
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.