In Java, int is represented by 32 bits, and long is represented by 64 bits. These two basic types are integers and have no other representation methods. How do they express themselves internally? It is represented by two's complement code. Since we often encounter Java's bit operations in solving problems, Java's bit operations can only be used for int and long types. So we have reason to analyze how Java is represented internally by binary. Here we only take int as an example, long is the same.
1. The range of int
int is represented by 32 bits internally in Java, and the highest bit represents the symbol, so the number that can really be represented is 31 Bit. The value range of Java's int type is -2^31~2^31-1.
Why is the range like this? This involves the binary storage method.
In Java, two's complement is used to store int. For a positive number, its complement is itself. For example, let's take 4 bits as an example. For positive numbers 1-7, they are 0001,0010, respectively. . . , 0111, a total of 7 numbers, which is 2^3-1.
For 0, you can see that the problem is coming. It can be -0 or 0, both are 0. Decide how to represent 0? The law of complement codes stipulates that 0 is considered to be 0, so 0 is represented by 0000.
At this time, it is necessary to express negative numbers. We believe that the complement representation of a negative number is the inversion of its absolute value (inversion along with the sign bit) plus 1. For example, for -5, its absolute value is 5, which is expressed as 0101. If it is inverted, it is 1010, and plus one is 1011. This is the complement representation of -5.
At this time, 1000 can be used to represent -8, so the range of negative numbers is -2^31, which also explains the value range of int. The same is true for 64-bit long.
2. Java’s bit operations
How is Java’s bit operation performed? Remember, a shift operation moves the entire number to the left or right as a whole. If it is shifted to the left, the low bit is filled with 0. If it is moved to the right, there are two situations. If it is 0 and a positive number, the high bits are filled with 0. If it is a negative number, the high bit is filled with 1. For example, -5 is 1011, and shifting one position to the right is 1101, which is -3.
There is also a bit operation. If it is a bitwise operation, remember that the sign bit also needs to be involved!
3. The relationship between right shift and division
For positive numbers (of course 0 is also the case), shifting one position to the right is equal to the result of dividing by 2, but for positive numbers Not for negative numbers (the result of dividing by 2 -1 to be exact)! For example, -5/2 results in -2, and -5>>1 results in -3.
4. The relationship between positive and negative signs and integer division and remainder operations
Positive numbers/Positive numbers, round down
Negative numbers/ Negative numbers are equal to dividing their absolute values
Positive/negative numbers and negative/positive numbers are equal to dividing their absolute values multiplied by the negative sign.
As for the results of %, the absolute value of the result is the same as the modulus of their absolute value, but the sign is determined by the first number.
5. Why use complement code?
(1) As mentioned before, in order to reasonably represent 0 and -0;
(2) You can express one more negative number at a negative number;
(3) Using complement code, the sign bit and other bits can be processed uniformly; at the same time, subtraction can also be processed as addition. In addition, when two numbers expressed in two's complement are added, if there is a carry in the highest bit (sign bit), the carry is discarded.
In fact, a big reason is to facilitate calculation of addition and subtraction. When using the two's complement system for operations, the sign bits can be added together for operation. In bit operations, if there is a carry in the sign bit, it can be discarded because it has exceeded the bit range. There's nothing special about positive numbers and their addition. The examples below are all 8 bits. For example, for 7 7, which is 00000111 00000111, the result is 00001110, which is 14.
For subtraction, for example, 9-4, we think it is 9 (-4). First, invert 4, which is 00000100, and add one to get the complement of -4, which represents 11111100, and then add it. , that is,
00001001(9)
11111100(-4)
? 100000101
The carry of the last sign bit is 1, because it has exceeded has exceeded the bit range, so discard it directly and get 5 directly. So this is the realization of subtraction changing to addition.
The above is the detailed content of what is java int. For more information, please follow other related articles on the PHP Chinese website!