shift : Java Glossary

go to home page S words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-03-12 Roedy Green, Canadian Mind Products
shift
Java has a number of operators for multiplying/dividing by powers of two, i.e. shifting left or right by bits. These are very fast operations, much faster than the using multiplication, division and Math.pow.
Shift Operators
Operation Operator Example Effect
left shift << 0x0a << 40xa0 Shift left n bits, multiply by 2n
logical right shift >>> 0xa0 >>> 40x0a
0xffffffa0 >>> 40x7ffffffa
unsigned shift right n bits, 0 bit fills in on the left, divide by 2n
arithmetic right shift >> 0xa0 >> 40x0a
0xffffffa0 >> 40xfffffffa
signed shift right n bits, sign bit fills in on the left, divide by 2n
left shift <<= x <<= 4 shorthand for x = x << 4
logical right shift >>>= x >>>= 4 shorthand for x = x >>> 4
arithmetic right shift >>= x >>= 4 shorthand for x = x >> 4

Creating Masks Dynamically

You can create powers of two, (one-bit masks) by shifting 1 left a variable amount, e.g. (1<<n). Normally you create masks with hex literals, e.g. 0x007f. To dynamically create a mask with n low order 1’s, use (1<<n)-1. Beware, for int this will not work for n=32 since shifts are done modulo 32.

Shiftless Shift

You would think that 1 << 66 would shift the one right off the left end and give you a result of 0. But int shifts are done modulo 32, so that is equivalent to 1 << 2 giving you a result of 4. Similarly, long shifts are done modulo 64 bits.

The shift operators can give some surprising results. You can’t shift ints by more than 31 bits. If you try it, you will shift by that amount modulo 32. Similarly you can’t shift longs my more than 63 bits. If you try it, you will shift by that amount modulo 64. This applies to <<<, << and >> So:

int i = -1;
i = i >>> 32;
// i is -1, not 0 as you might expect.

long k = 1;
k = k << 65;
// k is 2, not 0 as you might expect.
As a corollary, shifting left 32 bits does not clear an int. It is a no-op! Be especially careful when working with ints that will eventually be promoted to longs.

Be especially careful with any calculated shift operands. The shift modulo behavior makes creation of bit selections masks unnecessarily tricky. Suppose you want a general way of extracting bits i through j of an int. An obvious way to do it is to right shift the input by j bits, then & it with a mask containing j-i+1 one bits at the low order end. An obvious way to construct an n-bit right justified mask is (1<<n)-1. This is not correct in Java for n==32. (1 <<32) is 1, not 0, so subtracting 1 from it will result in a zero mask rather than an all-ones mask.

Java works this way for speed since many CPUs work that way.

Sun RFE number 4495754 : Shift fixed for n==32
In the meantime here is a little class you can use:


CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/shift.html J:\mindprod\jgloss\shift.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.105]
You are visitor number 11.