|left shift||<<||0x0a << 4 ⇒ 0xa0||Shift left n bits, multiply by 2n|
|logical right shift||>>>||0xa0 >>> 4 ⇒ 0x0a
0xffffffa0 >>> 4 ⇒ 0x7ffffffa
|unsigned shift right n bits, 0 bit fills in on the left, divide by 2n|
|arithmetic right shift||>>||0xa0 >> 4
0xffffffa0 >> 4 ⇒ 0xfffffffa
|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|
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.
The value of the shift should be in the range 0..31. You can’t reverse the direction of the shift by using a negative count. If you use a number outside the range, it will be corralled back into the range 0..31 by taking a modulo 32. 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. This applies to <<<, << and >>. 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.
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 (Central Processing Units) work that way.
In the meantime here is a little class you can use:
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[188.8.131.52]
|Feedback||You are visitor number 11.|