Good Java programmer learning routes Share displacement operation in Java

Good Java programmer learning routes Share displacement operation in Java, in Java, the displacement operation belong to the basic operations, symbols << and >> is that the displacement of the left and right displacement. Only integers can shift, so others do not consider in Java, the shift operation is a binary integer representation of the displacement in memory, so the displacement is divided into positive and negative numbers in Java.

For positive numbers, a leftward displacement, corresponding to << i.e. multiplied by 2, i.e., how many times the mobile is multiplied by the number of times 2, i.e., for example, 5 5 << 2 * 2 * 2 = 20, i.e., rightward displacement >> equivalent divided by 2, attention is integer divided by an integer. I.e., for example, 5 5 >> 1/2 = 2.

The following example:

5 << 1, we int as an example:

Number 5

int type is represented as binary: 0,000,000,000,000,000 0,000,000,000,000,101

I.e., to the left 1: 0000 0000 0000 0000 0,000,000,000,001,010 (posterior patch 0)

So the result is: number 10

5 >> 1, we int as an example:

Number 5

int type is represented as binary: 0,000,000,000,000,000 0,000,000,000,000,101

I.e., one to the right: 0000 0000 0000 0000 0,000,000,000,000,010 (high bit 0)

So the result is: the number 2

So for a positive number, you are free to use the displacement, and the results of multiplication and division is almost no difference, but better performance.


As for the negative it is, in Java displacement should be used with caution, because the displacement operation is called displacement signed in Java. Then signed displacement in the end is how to calculate it, directly illustrate the following:

-5 << 1, we int, for example, due to the negative presence in the memory is the complement, see:

Digital -5

int type binary original code is expressed as: 1,000,000,000,000,000 0,000,000,000,000,101

And its inverted is: 1,111,111,111,111,111 1,111,111,111,111,010

Complement is: 1,111,111,111,111,111 1,111,111,111,111,011

I.e., one to the left results: 1,111,111,111,111,111 1,111,111,111,110,110 (posterior patch 0)

The calculated result is inverted: 1,111,111,111,111,111 1,111,111,111,110,101

The results are calculated as the original code: 1,000,000,000,000,000 0,000,000,000,001,010

So the result is: digital -10

It looks like or multiplied by 2, but another number to try:

For example, if the binary digits is: 1,110,000,000,000,000 0,000,000,000,000,001

And its anti-code is: 1,001,111,111,111,111 1,111,111,111,111,110

Complement is: 1,001,111,111,111,111 1,111,111,111,111,111

I.e., one to the left results: 0,011,111,111,111,111 1,111,111,111,111,110

The most significant result is 0, so must be a positive number, so the result is not imagine multiplied by two.


If it is right, then the high will be supplemented by one, the result is not the same as normal, for example:

-5 >> 1, we int, for example, due to the negative presence in the memory is the complement, see:

Digital -5

int type binary original code is expressed as: 1,000,000,000,000,000 0,000,000,000,000,101

And its inverted is: 1,111,111,111,111,111 1,111,111,111,111,010

Complement is: 1,111,111,111,111,111 1,111,111,111,111,011

I.e., to a right result: 1,111,111,111,111,111 1,111,111,111,111,101 (high S.1)

The calculated result is inverted: 1,111,111,111,111,111 1,111,111,111,111,100

The results are calculated as the original code: 1,000,000,000,000,000 0,000,000,000,000,011

So the result is: the number -3

In Java, the result should be divided by 2 -5 -2, -3 but they displacement.

Further, if the following example:

-1 >> 1, we int, for example, due to the negative presence in the memory is the complement, see:

Number 1

int type binary original code is expressed as: 1,000,000,000,000,000 0,000,000,000,000,001

And its anti-code is: 1,111,111,111,111,111 1,111,111,111,111,110

Complement is: 1,111,111,111,111,111 1,111,111,111,111,111

I.e., to a right result: 1,111,111,111,111,111 1,111,111,111,111,111 (high S.1)

So the results do not count slowly to know is: Digital -1

So for -1, no matter how right, the result is -1, and if it is divided by 2, the result is 0.

A concluded, if positive, can be used when a shift operation may be possible to use, can improve the performance of, and for negative numbers, it is possible not to use it, is far from the division result.


Guess you like

Origin blog.51cto.com/14479068/2427704
Recommended