How values in Java are represented in a computer

Source: Internet
Author: User

Many children who have just studied Java do not understand why the int is sometimes turned into a negative number when strongly converted to byte.

This is because you do not understand the representation of numeric values in Java.

java specifies a negative number in the form of twos complement.

With the concept above, we know why the high-precision conversion of low-precision types can sometimes become negative.

For example, int 232 to byte value becomes-24

The conversion process is as follows:

232 is stored in the computer as follows:

00000000 00000000 00000000 11101000

To byte after the high-level storage is as follows:

11101000

The highest bit of this value is 1: So he is a negative number, expressed in twos complement. Then we can figure out what the value of this negative number is.

The inverse code is:

10010111

The original code is:

10011000

So his value is:-24

How values in Java are represented in a computer

Related Article

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.