Dark Horse Programmer--java Foundation--negative integer correlation calculation and its cause

Source: Internet
Author: User

-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------

  Time--January 6, 2016 15:33:02

1, integer class range of values and their analysis?

we know that the range of values for shaping is -2^31~2^31-1, but I wonder why negative numbers are not minus 1, and integers are minus 1. In addition to the left can be calculated using arithmetic progression its maximum value is indeed 2^31-1. But when I went to calculate the negative, I was wondering. We know that the sign bit of positive negative number is the 31st position, and the sign bit is not involved in the operation. Some friends may say that 10000000000000000000000000000000 is just -2^31, this is not exactly explained. If that's the case, it doesn't explain that 10000000000000000000000000000001 is -2^31+1. How do we know that the minimum negative positive number is -2^31, since it cannot be calculated? As a result, we know that the plastic is 32 bits, except   for the removal of the sign bit only 31 bits, each two possible, so altogether 2^31 may, positive integers starting from 0, so the maximum number is 2^31-1 (the array has five elements, the largest small mark is 41 principle), and negative integers start with-1, so it's -2^31.

2, why the negative integer can not use a positive integer calculation method?

  As for the binary representation of negative numbers, the computer engineer wanted the smallest negative integer to be -2^31, and that must have been in addition to the sign bit other bits nothing, that must be 10000000000000000000000000000000, And a little larger than the minimum of 1 on the right side plus one, and so on, and finally 12 binary 11111111111111111111111111111111, so see a binary is the most primitive calculation method, except that the sign bit as an integer, calculate the result after subtracting 2^ 31 (plus the minimum), but that sort of calculation is in too much trouble.

3, how to write a negative number of the binary form?

 We observed that the binary code of 1 and 0 is the inverse code,-the binary code of 2 and 1 is reciprocal code, and so on,-(x+1) and X code reciprocal, so want to ask for a negative binary code, must find the code and its reciprocal integer code, and then take the inverse code operation. If 8 of the code is to write 7 of the code first, and then take the inverse code.

4, see a negative number of binary how to calculate its decimal value?

  We know from the third, see a negative binary can first take the inverse code, calculate a positive number, then add 1, take negative number on OK. -(x+1) and X's code are mutually reversed. That can be seen as a negative code first to take the anti-code, calculate the X, and then add 1 to take negative numbers, the original-(x+1).

5, take the inverse code of the fast operation

  We know-(x+1) and X code each other is anti-code, so an integer anti-code is first plus 1 in the negative, a negative number of the anti-code is added 1 to take negative numbers, so only need to add 1 negative.

6. Summary :

In the past, I always do not know, although these things are now packaged as a method, completely without their own calculations. But still, I like the process of solving problems that have plagued me for years, and it's cool. Come on, Lee Chong.

  

  

Dark Horse Programmer--java Foundation--negative integer correlation calculation and its cause

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.