Negative Numbers and type conversion problems in Java, negative type conversion in java
All those who have learned about java know that in java, it is not an intuitive negative number, but a positive number.
This is for the convenience of hardware operations, and the subtraction is also converted into addition for calculation.
How is the complement representation? In order to get the complement code, we introduced the reverse code.
For a positive number, its anticode complement is itself. If we don't understand why, we can understand it as follows: the reason for introducing the anticode complement is to solve the problem of subtraction, in other words, the number of sentences solves the problem of negative numbers in java. Positive numbers do not have these problems, so its anti-Code complement is itself. In a symbolic basic data type, the highest bit 0 indicates a positive number, and the highest bit 1 indicates a negative number. For a negative number, its anticode is to remove the inverse of the symbol bit, and then add 1 to get its complement code.
Here is a simple example. A byte data occupies 8 digits in the computer.-7 can be expressed as 10000111, and 1 in the highest digit represents a negative number, the anti-code is used to remove the symbol bits. You can get the anti-code 11111000 by adding 1.
The binary value of 8 is 00001000. Now we calculate 8-7. In the computer, instead of 8 minus 7, we use 8 + (-7 ), that is to say, add the-7 complement code 00001000 with 11111001 and add the two signed numbers. If the signed bits are added with near bits, delete the carry number of the signed bits and get 00000001, that is, 1.
In java, we often encounter conversions of different data types, which is the most error-prone.
First, let's take a look at what is a symbolic extension. For a positive number, add 0 in front and 1 in front of a negative number. For example, if the 8-bit binary number 10000111 is extended to 16 bits, we add 8 first, 111111111_111; if it is a positive number, we add 0 in front. After expansion, the size of the symbol and value remains unchanged.
Next, let's take a look at how different types are converted. First, we need to know each of these basic types occupies several places. Signed: byte = 8 int = 32 short = 16 long = 64 float = 32 double = 64 * boolean occupies only one digit, and 0 and 1 represent false and true.
Unsigned: char = 16
1. the byte type is converted to the char type because the byte type is a signed type and needs to be expanded to the char type. If it is a positive number, there will be no more than eight zeros in front, if it is a negative number, add the first eight 1. For example, if the left side of 11111111 (0xff) is consecutively supplemented with eight values, the result is 0 xffff. Because char is of the unsigned type, 0xffff indicates the decimal number of 65535.
2. convert the char type to the int type because the char type is unsigned. When the char type is converted to the int type, add 16 zeros to the front. The result is 0x0000ffff in decimal format, and the corresponding decimal number is 65535.
3. The int type is converted to the byte type because the int type is 32 bits, while the byte type value only occupies 8 bits, and the last 8 bits are intercepted directly. For example, if the complement code of-1 is 0 xffffffff, It is 0xff after being converted to byte type, and the value is-1.
Summary: if the initial numeric type is signed, the symbolic extension is executed. If the numeric type is char, the zero extension is executed no matter what type it is converted. Another rule should also be noted that if the length of the target type is smaller than that of the source type, the length of the target type is intercepted directly. For example, to convert the int type to the byte type, the right eight digits of the int type are intercepted.