Java Garage Authors: Hewitt E. Published year: 2006 Pages: 35-36/228

### Shift Operators

There are three shift operators, used for changing the bit values in integral types. They aren't used frequently, except in programs that need to keep their memory usage to an absolute minimum. The shift operators are

1. The left shift, represented as <<

2. The signed right shift, represented as >>

3. The unsigned right shift, represented as >>>

The left-hand side of the operators is the value to be shifted, and the right-hand side is the number of places to shift it. The type of each operand has to be an integral type, or a compiler error will occur. The shift happens on the 2's complement integer representation of the value on the left.

When the value to be shifted is an int , only the last 5 digits of the right-hand operand are used in performing the shift. When the value to be shifted is a long , only the last 6 digits of the right-hand operand are used in performing the shift.

The shifts occur at runtime, on a bit-by-bit basis.

Let's show the bits for the number 13, which we learned how to do in the last section.

 64 32 16 8 4 2 1 1 1 1 = 13

If you shifted all of the bits that make up that number 1 to the left, what would you have? You'd have this:

 64 32 16 8 4 2 1 1 1 1 = 26 (because 2 + 8 + 16=26)

We have the left shift operator to do this work for us, and we can represent it in Java code like this: (13 << 1).

What if we shifted 13 over 1, but to the right?

 64 32 16 8 4 2 1 1 1 = 6

Wait a moment. What happened to the 1 value that we originally had sitting under the 1 position? It just dropped off the map. It is forgotten about completely, and we have only what's left. If we shift 13 over to the right 10 positions, or even 938 positions guess what we end up with? 0. That's because the regular right shift operator ( >> ) is signed. Use the unsigned right shift operator to move 13 over 1 place, and you get the same result (6).

So what if we use a negative number to start with? For example, (-13 >>> 2)? Should be a fairly similar, innocuous result, and then we can call it quits. I'm afraid that (-13 >>> 2) = 1073741820.

What in the world will become of us?

Well.

The value of num<<pos is num shifted to the left pos positions.

The value of num>>pos is num shifted to the right pos positions, with the sign extension retained. The result is num/2 pos. For non-negative numbers , performing a right shift is equivalent to dividing the left-hand operand by 2 to the power of the right-hand operand.

The value of num>>>pos is num shifted right pos positions with 0 extension. For all positive num s, the result is the same as using the >> operator. The difference between >>> and >> is that >> fills in all of the left bits with zeros. That means that the sign value can change. For negative num s, the result is equivalent to num right-shifted by pos plus two left-shifted by the inverted value of the right-hand operand.

There are limited contexts in which you need to use the shift operators. Many of the Java APIs shield us from having to deal with low-level bit manipulation tasks , so we can probably quit talking about them now. Anyway, more talk at this point would just be more of the same.

## DO OR DIE:

• To be the smooth jazz of Java chapters

Control statements are the things you write to control the flow of your program. They're pretty straightforward for the most part, and very similar to how these constructs are implemented in other languages.

So I am going to go out on a limb here. I am betting that you're going to be familiar with when and why to use stuff like if/else statements and for loops . Maybe we can spare ourselves the formality . If you're not familiar, that's okay. These are basic elements in any programming language, and there are hundreds of examples of their usage throughout the remainder of this book.

 Java Garage Authors: Hewitt E. Published year: 2006 Pages: 35-36/228