Java Thousands asked _06 data structure (024) _ Binary How to represent integer values

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. How to represent integer values with binary

We all know that computers only know 0, 12 binary, we generally operate registers and storage units are only known binary, we call a binary bit (bit), the general 32-bit computer registers allow the operation of 32bit data, that is 32 0 or 1, because the writing is too long, We generally use hexadecimal notation (every two hexadecimal becomes a byte byte, i.e. 8bit=1byte). For example:

1111 1111 1111 1111 1111 1111 1111 1111 = ffffffff

Learn about 32-bit and 64-bit computers See here: What is the difference between 32-bit and 64-bit computers

Java is the same, the above integer value will eventually be interpreted as binary machine code, the following rules:

    1. The first is the sign bit, 1 for negative, 0 for positive.

    2. The binary complement of positive values, which is the corresponding negative value.

    3. The range of different types is -2^ (n-1) to 2^ (n-1)-1, where n is the number of bits occupied by the type.

One of the mentioned complement, that is, anti-code +1, anti-code is the original code of each bits to take the inverse, the length of the 8bit integer as an example:

Original code: 0100 1010

Anti-code: 1011 0101

Complement: 1011 0110

2. How integer types in Java are represented by binary

In the Java language, integer values are divided into 4 types: Byte, short, int, long, all of which are signed integers.

See the basic data types here: What are the 8 basic data types in Java
These basic types in addition to the length of the inconsistency, the other rules are in accordance with the above rules, specifically as follows:

byte
Memory occupies 1 bytes, 8bit. Because it is a signed value, it can only use 7bit binary to represent the size, the maximum can only represent 111 1111, the smallest is its complement of 1000 0000, so his range is:

1 000 0000 to 0 111 1111

Among them, the first sign bit, 1 for negative, 0 for positive, so the conversion to 10 binary namely-128 to 127.

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception {byteB1 = -;byteb2 =- -; System. out. println ("Positive byte==="+ integer.tobinarystring (B1));//Turn binarySystem. out. println ("Negative byte==="+ integer.tobinarystring (B2)); System. out. println ("Positive byte==="+ integer.tohexstring (B1));//Turn hexSystem. out. println ("Negative byte==="+ integer.tohexstring (b2));}}

The results are as follows:
Zheng byte===1111111
Negative byte===11111111111111111111111110000000
Zheng byte===7f
Negative byte===ffffff80

Here we see that byte has a 32bit length through the tool of the integer class, in fact, in Java, both byte and short are assigned via int, positive value is 0 on the front end, but when the stack is actually pressed into the heap or as an object's property, will only occupy the corresponding length: Byte occupies 8bit,short occupies 16bit. For example:

byte0x81;//编译错误byte0xffffff81;//正确

Understanding byte, short to memory consumption look here: [Byte, short exactly how much memory][4]

Short
Memory occupies 2 bytes, 16bit. With Byte, signed value, using 15bit binary to represent the size, the maximum can only represent 111 1111 1111 1111, the smallest is its complement of 1000 0000 0000 0000, so his range is:

1 000 0000 0000 0000 to 0 111 1111 1111 1111

Among them, the first sign bit, 1 for negative, 0 for positive, so the conversion to 10 binary namely-32768 to 32767.

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception { ShortS1 =23235; ShortS2 =-23235; System. out. println ("Positive short==="+ integer.tobinarystring (S1));//Turn binarySystem. out. println ("Negative short==="+ integer.tobinarystring (s2)); System. out. println ("Positive short==="+ integer.tohexstring (S1));//Turn hexSystem. out. println ("Negative short==="+ integer.tohexstring (s2));}}

The results are as follows:
Zheng short===101101011000011
Negative short===11111111111111111010010100111101
Zheng Short===5ac3
Negative short===ffffa53d

Similarly, short in Java can only be assigned by Int.

int
Memory occupies 4 bytes, 32bit. Signed value, using the 31bit binary to represent the size, the rule is the same, his range is:

1 000 0000 0000 0000 0000 0000 0000 0000 to 0 111 1111 1111 1111 1111 1111 1111 1111

Among them, the first sign bit, 1 for negative, 0 for positive, so the conversion to 10 binary namely-2,147,483,648 to 2,147,483,648.

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception {intI1 =328999123;intI2 =-328999123; System. out. println ("Positive int==="+ integer.tobinarystring (i1));//Turn binarySystem. out. println ("Negative int==="+ integer.tobinarystring (i2)); System. out. println ("Positive int==="+ integer.tohexstring (i1));//Turn hexSystem. out. println ("Negative int==="+ integer.tohexstring (I2));}}

The results are as follows:

Zheng int===10011100111000010000011010011
Negative int===11101100011000111101111100101101
Zheng Int===139c20d3
Negative int===ec63df2d

Long
Memory occupies 8 bytes, 64bit. Signed value, using the 63bit binary to represent the size, the rule is the same, his range is:

1 000 0000 0000 0000 0000 0000 0000 0000 to 0 111 1111 1111 1111 1111 1111 1111 1111

Among them, the first sign bit, 1 for negative, 0 for positive, so the conversion to 10 binary namely-2,147,483,648 to 2,147,483,648.

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception {intI1 =328999123;intI2 =-328999123; System. out. println ("Positive int==="+ integer.tobinarystring (i1));//Turn binarySystem. out. println ("Negative int==="+ integer.tobinarystring (i2)); System. out. println ("Positive int==="+ integer.tohexstring (i1));//Turn hexSystem. out. println ("Negative int==="+ integer.tohexstring (I2));}}

The results are as follows:

Zheng int===10011100111000010000011010011
Negative int===11101100011000111101111100101101
Zheng Int===139c20d3
Negative int===ec63df2d

Here is to say, for 32-bit machine, because the register instruction can only accept 32bit value, so the 64bit long in the operation can only be segmented processing, performance is relatively slow. Therefore, it is best not to use a long and double 64bit type in a 32-bit machine. and 64-bit computers can be used perfectly.

Java FAQ _06 Data Structure (024) _ Binary How to represent integer values

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.