How to store integer types (short int long) in java

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

The other three types are as follows:

1.
Basic type: short Binary digits: 16
Packaging class: java.lang.Short
Minimum value: Short.MIN_VALUE=-32768 (-2 to the 15th power)
Maximum value: Short.MAX_VALUE=32767 (2 to the 15th power -1)

2.
Basic type: int Binary digits: 32
Packaging class: java.lang.Integer
Minimum value: Integer.MIN_VALUE= -2147483648 (-2 to the 31st power)
Maximum value: Integer.MAX_VALUE= 2147483647 (2 to the 31st power -1)


3.
Basic type: long Binary digits: 64
Packaging class: java.lang.Long
Minimum value: Long.MIN_VALUE=-9223372036854775808 (-2 to the 63rd power)
Maximum value: Long.MAX_VALUE=9223372036854775807 (2 to the 63rd power -1)

Take the short type as an example:

  First of all, you need to understand that the highest bit in the computer is the sign bit, 0 for positive numbers and 1 for negative numbers. In the computer, the data is represented by its complement , but the complement of the positive number is itself, and the complement of the negative number is the complement obtained by inverting the source code of the negative number and adding one.

    1. The original code, inverse code, and complement code of positive numbers are all equal 
    Example: 0000 1010 (represents +10 in decimal, the first bit on the left is the sign bit) 
    its original code, inverse code, and complement code are all 0000 1010 
    2. The original code of a negative number is itself, the inverse code of a negative number is to keep the sign unchanged, and invert the number after the sign. For the complement code of a negative number, invert the original code and add 1 
    Example: 1000 1010 (in decimal Represents -10) 
    its original code is 1000 1010 
    its complement is 1111 0101 
    its complement is 1111 0110

    For example, short type: -1 Binary designation: 10000000 00000001 (the largest negative integer -1)

          Negate: 11111111 11111110  

          Add 1's complement: 11111111 11111111 (the marking method of the largest negative integer -1 in the computer)

After understanding the above, let's talk about the binary designation of short:

    Look at this first: The smallest negative integer -32768 Binary notation in the computer: 10000000 00000000 

          The largest negative integer -1 computer binary notation: 11111111 11 11 1111 

          0 Binary notation in the computer: 00000000 0000000 

         The smallest positive integer 1 in computer binary notation: 00000000 00000001

         The largest positive integer: 32767 Binary notation in the computer: 01111111 11111111 

After the smallest negative number -32768 is added to 1, the binary mark in the computer is: 10000000 00000001 Keep adding 1 until 11111111 11111111 (-1) reaches the largest negative integer, and then add 1 to become: 1 00000000 0000000    Note that the byte length here is 17 bits, and the short type only takes 16 bits, namely: 00000000 0000000, so after -1+1, it becomes 0, then 00000000 00000000 is incremented by 1 until 01111111 11111111 reaches the maximum positive integer (32767), and 32767 plus 1 becomes: 10000000 00000000 That is, the smallest negative integer -32768 (2^15)

As shown in the figure:  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326348782&siteId=291194637