Why is the value range of Java byte type -128~127 (RPM)

Source: Internet
Author: User
Tags bitwise

Concept: The complement of a negative number is the absolute value of the original code bitwise negation, and then the entire number plus 1

Steps:

1. Determine that byte is 1 bytes, or 8 bits

2. The largest should be 0111 1111, because the first bit is the sign bit, 0 indicates a positive number

3. The smallest should be 1000 0000, in the same vein, indicating the smallest negative number (1111 1111 is the largest negative number-1)

4.0111 1111 That's 127

5.1000 0000 According to the bottom of the explanation is first minus one, get 0111 1111, and then the bitwise negation of 1000 0000, the result is the absolute value of the desired negative, so the result is-128 (and the concept of inverse to find the negative number)

=========================================================

There are 3 representations of the fixed-point number in the computer: original code, inverse code, and complement

The [original code] is the binary fixed-point notation described earlier, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, and the remaining bits represent the size of the value.

[Anti-code] notation: The inverse of a positive number is the same as its original code; the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

The [complement] notation stipulates that the complement of positive numbers is the same as the original code, and the complement of negative numbers is added to the minus 1 of the inverse code.

====================================================

3.4.1 Complement

Java uses the complement to represent the binary number, in the complement representation, the highest bit is the sign bit, the positive sign bit is 0, the negative number is 1. The complement rules are as follows:

For positive numbers, the highest bit is 0, and the rest of you represent the value itself (in binary notation), such as +42 's complement of 00101010.

For negative numbers, the complement of the absolute value of the number is reversed, and then the whole number is added 1, which is the complement of the number. The complement of 42 is 11010110 (00101010 bitwise reverse 11010101 +1=11010110)

The number is expressed in the complement, and the complement of 0 is the only one, which is 00000000. (whereas in the original code, in the inverse code representation, the +0 and 0 representations are not unique, see the corresponding book). And you can use 111111 to denote-1 of the complement (this is also the difference between the complement and the original code and the inverse code).

From:http://blog.163.com/[email protected]/blog/static/47517863200911314245752/

Why is the value range of Java byte type -128~127 (RPM)

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.