Java programming: Integer range of values ( -2^31~2^31-1) analysis __ Algorithm

Source: Internet
Author: User

In Java, the minimum and maximum values for an Integer are defined as follows:

/**
 * A constant holding the minimum value an {@code int} can
 * have, -2<sup>31</sup>.
 * *
@Native public static final int   min_value = 0x80000000;

/**
 * A constant holding the maximum value an {@code int} can
 * have, 2<sup>31</sup>-1.
 * *
@Native public static final int   max_value = 0x7fffffff;

The minimum value is -2^31, the maximum value is 2^31-1, why? The derivation process is as follows: the number of digits in the binary code that the Integer actually occupies

An integer type occupies 4 bytes, one byte occupies 8 bits of binary, so an integer is a total of 32-bit binary code. Removes the first digit symbol bit and leaves 31 digits to represent the value. original code, inverse code, complement

In the computer, the data is stored by the binary complement, in the Java code we see "0x80000000", "0x7fffffff" are the form of complement, by converting to the original code to get their real value.

Min_value = 0x80000000;  Complement
max_value = 0x7fffffff;  Complement

Conversion formula: When the original code is positive, the original code, the inverse code, the complement is the same.

Positive number: 1
Original code: 0000 0000 0000 0000 0000 0000 0000 0001
Anti-code: 0000 0000 0000 0000 0000 0000 0000 0001
Complement: 0000 0000 0000 0000 0000 0000 0000 0001 When the original code is a negative number, the inverse code for the removal of sign bit by bit counter, complement for the removal of sign bit by bit and then add 1.

Negative Number:-1
Original code: 1000 0000 0000 0000 0000 0000 0000 0001
Anti-code: 1111 1111 1111 1111 1111 1111 1111 1110
Complement: 1111 1111 1111 1111 1111 1111 1111 1111

So in the program, when we define the number of 16-in-0x00000001, the 1,0XFFFFFFFF represents 1. The maximum value is 2^31-1, not 2^31.

Original code: 0111 1111 1111 1111 1111 1111 1111 1111
Anti-code: 0111 1111 1111 1111 1111 1111 1111 1111
Complement: 0111 1111 1111 1111 1111 1111 1111 1111

The maximum number of integers that can be represented in a computer is "0111 1111 1111 1111 1111 1111 1111 1111", the complement of a positive number is the same as the original code, and the original code is "0111 1111 1111 1111 1111 1111 1111", converted to decimal Number is 2^31-1. The minimum value is -2^31, not-(2^31-1)

We calculate the negative values in turn, as follows:

Original code: 1000 0000 0000 0000 0000 0000 0000 0001-1
Anti-code: 1000 0000 0000 0000 0000 0000 0000 0000
Complement: 1111 1111 1111 1111 1111 1111 1111 1111

Original code: 1000 0000 0000 0000 0000 0000 0000 0010-2
Anti-code: 1000 0000 0000 0000 0000 0000 0000 0001
Complement: 1111 1111 1111 1111 1111 1111 1111 1110

Original code: 1111 1111 1111 1111 1111 1111 1111 1111-2^31 + 1
Anti-code: 1111 1111 1111 1111 1111 1111 1111 1110
Complement: 1000 0000 0000 0000 0000 0000 0000 0001

Original code: 1000 0000 0000 0000 0000 0000 0000 0000-0 Convention for -2^31
Anti-code: 1111 1111 1111 1111 1111 1111 1111 1111
Complement: 1000 0000 0000 0000 0000 0000 0000 0000

It can be inferred from this that the minimum value of an Integer is -2^31.

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.