Negative numbers and type conversion issues in Java

Source: Internet
Author: User

Learned Java, in Java, is not a visual representation of negative numbers, but in the form of complement to represent negative numbers.

This is for the convenience of hardware operation, the subtraction is also converted to addition to operate.
What does that complement mean? In order to get the complement, we introduced the anti-code.

For a positive number, its anti-code complement is itself, if not understand why, we can understand that: the reason for the introduction of anti-code complement is to solve the problem of subtraction, the number of words is to solve the problem of negative numbers in Java, positive numbers do not exist these problems, so its anti-code is its own complement. In a signed base data type, the highest bit 0 represents a positive number, and the highest bit 1 indicates a negative number. For negative numbers, its inverse code is to remove the symbol bit negation, and then add 1 to get its complement.
Here is a simple example, a byte-type data, it occupies 8 bits in the computer, 7 can be represented as 10000111, the highest bit of 1 for the minus, its anti-code is to remove the sign bit to 11111000, and then add 1 to get the complement 11111001.

8 of the binary representation is 00001000, now we operate 8-7, in the computer is not 8 minus 7, but with 8 + (-7), that is, 00001000 plus-7 of the complement 11111001, two signed number added, if the symbol bit is added near the bit to delete the sign bit of the carry, Get 00000001, which is 1.
In Java we often encounter transformations of different data types, which is the most error-prone place.

First, let's look at what a symbol extension is, and for a positive number, fill 0 in front and 1 in front for negative numbers. For example, the 8-bit binary number 10000111 is expanded to 16 bits, we add 8 1,1111111110000111 in front, and if it is positive, it is preceded by 0. When this is expanded, the symbols and values are the same size.
Let's take a look at how the different types are converted, and first we need to know which of these basic types are each. Signed: byte = 8 int = Short = +-----+---------------------only one bit, with 0 and 1 for false and true.
Unsigned type: char = 16
1. Byte type to char type because byte is a signed type, and then to a char type, you need to expand the sign bit, if the positive number is not in front of 8 0, if the negative number on the front of 8 1. For example, 11111111 (0xff) left consecutively 8 1 results are 0xffff. Because char is an unsigned type, the decimal number represented by 0xFFFF is 65535.
2. Char to int type because char is an unsigned type, the conversion to int type is preceded by 16 0, the result of the decimal is 0x0000ffff, the corresponding decimal number is 65535.
3. int to byte type because int is 32 bits, and byte type value only 8 bits, intercept the last 8 bits directly. For example, 1 of the complement is 0xFFFFFFFF, converted to a byte type after 0xFF, the value is-1.
Summary: If the initial numeric type is signed, the symbol extension is performed, and if it is a char type, the 0 extension is performed regardless of what type it is being converted to. Another rule to remember is that if the length of the target type is less than the length of the source type, the length of the target type is directly intercepted. For example, the int type is converted to a byte type, and the right 8 bits of the int are intercepted directly.  ?

Negative numbers and type conversion issues 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.