The Shift Operators and

suggest change

The Java language provides three operator for performing bitwise shifting on 32 and 64 bit integer values. These are all binary operators with the first operand being the value to be shifted, and the second operand saying how far to shift.

Notes:

  1. These operators require an int or long value as the first operand, and produce a value with the same type as the first operand. (You will need to use an explicit type cast when assigning the result of a shift to a byte, short or char variable.)
  2. If you use a shift operator with a first operand that is a byte, char or short, it is promoted to an int and the operation produces an int.)
  3. The second operand is reduced modulo the number of bits of the operation to give the amount of the shift. For more about the mod mathematical concept, see Modulus examples.
  4. The bits that are shifted off the left or right end by the operation are discarded. (Java does not provide a primitive “rotate” operator.)
  5. The arithmetic shift operator is equivalent dividing a (two’s complement) number by a power of 2.
  6. The left shift operator is equivalent multiplying a (two’s complement) number by a power of 2.

The following table will help you see the effects of the three shift operators. (The numbers have been expressed in binary notation to aid vizualization.)

Operand1 | Operand2 | << | >> | >>> | | —— | —— | —— | —— | —— | —— | 0b0000000000001011 | 0 | 0b0000000000001011 | 0b0000000000001011 | 0b0000000000001011 | no change | 0b0000000000001011 | 1 | 0b0000000000010110 | 0b0000000000000101 | 0b0000000000000101 | | 0b0000000000001011 | 2 | 0b0000000000101100 | 0b0000000000000010 | 0b0000000000000010 | | 0b0000000000001011 | 28 | 0b1011000000000000 | 0b0000000000000000 | 0b0000000000000000 | | 0b0000000000001011 | 31 | 0b1000000000000000 | 0b0000000000000000 | 0b0000000000000000 | | 0b0000000000001011 | 32 | 0b0000000000001011 | 0b0000000000001011 | 0b0000000000001011 | 32 mod 32 is 0 | … | … | … | … | … | | 0b1000000000001011 | 0 | 0b1000000000001011 | 0b1000000000001011 | 0b1000000000001011 | no change | 0b1000000000001011 | 1 | 0b0000000000010110 | 0b1100000000000101 | 0b0100000000000101 | note sign extension for ‘>>’. 0b1000000000001011 | 2 | 0b0000000000101100 | 0b1110000000000010 | 0b00100000000000100 | note sign extension for ‘>>’. 0b1000000000001011 | 31 | 0b1000000000000000 | 0b1111111111111111 | 0b0000000000000001 | note sign extension for ‘>>’. |

There examples of the user of shift operators in http://stackoverflow.com/documentation/java/1177/bit-manipulation#t=201610101439344327372

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Operators:
* The Shift Operators and

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
39 Operators
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap