"Original" Java Shift operations

Source: Internet
Author: User
Tags number sign

To learn the shift operation, you first need to know the number of bits involved in the shift operation, so let's review the placeholder for the Java base type.

    • Java base type
Summary List of Java Foundation types
Type Number of binary digits Maximum Value Minimum value Initialize value Representation with symbols
Char 8 127 (2^7-1) -128 ( -2^7) 0 Two-step complement Is
Short 16 32767 (2^15-1) -32768 ( -2^15) 0 Two-step complement Is
Int 32 2,147,485,647 (2^31-1) -2,147,483,648 ( -2^31) 0 Two-step complement
Long 64 9,223,372,036,854,775,807 (2^63-1) -9,223,372,036,854,775,808 ( -2^63) 0L Two-step complement Is
Float 32 3.4028235E38 1.4E-45 0.0f IEEE 754 standard floating-point number Is
Double 64 1.7976931348623157E308 4.9E-324 0.0f IEEE 754 standard floating-point number Is
Boolean Pending analysis Only true and False Only true and False False The JVM uses 0/1 to indicate

The difference between float and double is that one is a single-precision floating-point number, and the other is a double-precision floating-point number.

A floating-point number is a representation of an exponential type within a machine, which can be decomposed into four parts: a symbol, a mantissa, an exponential symbol, an exponent

Both the number sign bit and the exponent sign bit are one, indicating positive or negative.

For float, the exponent portion occupies 8 bits, wherein the exponent symbol is one digit, the value part is 7 bits, the tail part occupies 24 bits; for double, the exponent portion is 16 bits, the exponent part is 15 bits, and the tail part occupies 48 bits. So double is larger and more accurate than the number represented by float, but at the same time it is twice times the memory consumption.

The Boolean majority problem depends on the situation, in the JVM, the operation of the Boolean value is replaced by the int type, so it occupies 32 bits; If you define a byte array of type Boolean, the JVM compiles it to a byte array type, which takes up 8 bits. In fact, here also listen to doubts, why a Boolean can occupy 8, one will occupy 32? Personal understanding of the feeling is that when the real Boolean, should be only 1 bits, not 0 is 1, but the specific compile time other occupancy may be used for other purposes.

OK, next, let's look at the problem with the shift operation.

the shift operators for Java include  three kinds:, >> (with symbol right shift) and >>> (unsigned Right shift).

    • left shift operator <<

Usage: value << number

Principle: Discard the highest bit, low 0

Note: The Char,byte and short type are shifted to the int type before the shift operation.

When the number of bits moved exceeds the number of digits of the number of moves, a modulo operation, such as 45<<34, is moved 2 bits, because 34%32=2.

Instance:

1. Example procedures

 Public class Test {    publicstaticvoid  main (String args[]) {          int num =0x40000000;           SYSTEM.OUT.PRINTLN (num);          = num << 1;         SYSTEM.OUT.PRINTLN (num);    }}

2. Program Results

1073741824-2147483648

A typical use case is selected, which shows that the symbol bit is also moved together, the number bit is 1 and moves to the sign bit, which turns the numbers into negative numbers.

    • Right shift with symbols >>

Usage: value >> number

Principle: The symbol bit is unchanged, the left side of the symbol is added, and at the same time the lowest bit to discard

Note: The Char,byte and short type are shifted to the int type before the shift operation.

When the number of bits moved exceeds the number of digits of the number of moves, a modulo operation, such as 45<<34, is moved 2 bits, because 34%32=2.

Instance:

1. Example procedures

 Public class Test {    publicstaticvoid  main (String args[]) {          int NUM1 =0x80000000;            int num2 = 0x00000003;         System.out.println (NUM1);         System.out.println (num2);          = Num1 >> 2;          = num2 >> 1;         System.out.println (NUM1);         System.out.println (num2);    }}

2. Running Results

-21474836483-5368709121

NUM1 shows that the symbol moves to the right, with symbol movement.

Num2 shows that the lows are discarded and changed from 3 to 1.

    • Unsigned Right Shift >>>

Usage: value >>> number

Principle: The symbol is unchanged, the left side is 0, and at the same time the lowest bit to discard

Note: The Char,byte and short type are shifted to the int type before the shift operation.

When the number of bits moved exceeds the number of digits of the number of moves, a modulo operation, such as 45<<34, is moved 2 bits, because 34%32=2.

Instance:

1. Example procedures

 Public class Test {    publicstaticvoid  main (String args[]) {          int NUM1 =0x80000000;            int num2 = 0x00000003;         System.out.println (NUM1);         System.out.println (num2);          = NUM1 >>> 1;          = num2 >>>;         System.out.println (NUM1);         System.out.println (num2);    }}

2. Running Results

-2147483648310737418241

As you can see from NUM1, the sign bit also moves along with the digital digits.

"Original" Java Shift operations

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.