How the integer type (short int lang) is stored in Java

Source: Internet
Author: User

There are four types of integers in Java, namely byte short int long where byte has only one byte 0 or 1, which is not explained in detail here.

The other three types are as follows:

1.
Basic type: Short bits number: 16
Packing type: Java.lang.Short
Minimum value: short.min_value=-32768 (-2 of 15 this party)
Maximum value: short.max_value=32767 (2 of 15 square-1)

2.
Base type: int bits number: 32
Packing type: Java.lang.Integer
Minimum value: integer.min_value=-2147483648 (-2 of 31 squares)
Maximum value: integer.max_value= 2147483647 (2 of 31 square-1)


3.
Basic type: Long bits number: 64
Packing type: Java.lang.Long
Minimum value: long.min_value=-9223372036854775808 (-2 of 63 squares)
Maximum value: long.max_value=9223372036854775807 (2 of 63 square-1)

Take the Short type example:

The first thing to understand is that the highest position in the computer is the sign bit, 0 is a positive number, and 1 is negative. The data in the computer is represented by its complement , but the positive complement is its own, the complement of negative number is negative of the source of the reverse plus a get complement.

First, positive number of the original code, anti-code, complement are equal
Example: 0000 1010 (represents +10 in decimal, the first digit of the left is the sign bit)
Its original code, anti-code, complement is 0000 1010
Second, the negative number of the original code is its own, negative anti-code is the symbol will not change, the symbol behind the numbers to reverse, for the complement of negative numbers is the original code to take the reverse after adding 1
Example: 1000 1010 (represented in decimal-10)
It's the original code is 1000 1010
It's anti-code is 1111 0101
Its complement is 1111 0110.

For example Short type:-12 binary indication: 10000000 00000001 (Maximum negative integer-1)

Reverse: 11111111 11111110

Plus 1 complements: 11111111 11111111 (the largest negative integer-1 in the computer marking method)

Understand the above these two binary signs:

First look at this: the smallest negative integer-32768 binary in the computer: 10000000 00000000

Maximum negative integer-1 binary mark in computer: 11111111 one by one1111

0 binary indication in computer: 00000000 0000000

Minimum positive integer 1 in the computer binary indication: 00000000 00000001

Maximum positive integer: 32767 binary indication in computer: 01111111 11111111

Minimum negative number-32768 plus 1 after the computer binary is marked as: 10000000 11 straight plus 1 until to 11111111 11111111 (-1) to reach the maximum negative integer, and then add 1 to: 1 00000000 0000000 Note here The byte length is 17 bits, while the short type only takes 16 bits namely: 00000000 0000000 so the -1+1 becomes 0 then 00000000 00000000 is added 1 until 01111111 11111111 reaches the maximum positive integer (32767 ), 32767 plus 1 becomes: 10000000 00000000 is the smallest negative integer-32768 (2^15)

Marked:

How the integer type (short int lang) is stored in Java

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.