posted 17 years ago

The shift operators are designed to move bits around in a byte. For instance, 1 is represented as 0000 0001 in binary. Doing a shift left on this will move all the bits one place to the left, resulting in 0000 0010, or the number 2. The number 3 would be shown as 0000 0011. A shift left of one place on this returns 0000 0110, or the number 6. Hence, a left shift of one place effectively doubles a number.

Does this answer your question? Maybe if you posted the specific questions from your SCJP exam, we could be of more help.

- Don

Does this answer your question? Maybe if you posted the specific questions from your SCJP exam, we could be of more help.

- Don

SaiPrasad Jukalkar

Ranch Hand

Posts: 79

posted 17 years ago

Thank you Don, i have understood the left shift operators, in addition to this i want to know about right shift and unsigned shift operators, and is there any short-cut formula to solved these questions, because i have seen, some of the questions are very cryptic and lengthy. looking forward for a nice solution.

JSP

------------------

JSP

JSP

------------------

JSP

JSP

posted 17 years ago

Right shift is just the opposite of left shift. If you have a value 0000 1111 (15) and you right shift, it will become

0000 0111. The rightmost digit drops off and you are left with 7.

If you start with a negative number (the leftmost digit is a 1)

1000 1111 and right shift (this is a signed shift) it will become 1100 0111. The leftmost bit will retain the previous value.

If you do an unsigned right shift you would get 0100 0111 (the leftmost bit is always 0 no matter what it started as.

There is not really any short cut. It is straight forward, it's just that negative binarys are kind of messy to work with.

0000 0111. The rightmost digit drops off and you are left with 7.

If you start with a negative number (the leftmost digit is a 1)

1000 1111 and right shift (this is a signed shift) it will become 1100 0111. The leftmost bit will retain the previous value.

If you do an unsigned right shift you would get 0100 0111 (the leftmost bit is always 0 no matter what it started as.

There is not really any short cut. It is straight forward, it's just that negative binarys are kind of messy to work with.

"JavaRanch, where the deer and the Certified play" - David O'Meara

Cindy Glass

"The Hood"

Sheriff

Sheriff

Posts: 8521

posted 17 years ago

Well, if you are stuffing lots of code into a teeny tiny little chip, it is a great way to get lot's done with little space. It's amazing the clever things some people think up to use bit switching.

Myself, I try to avoid it.

Myself, I try to avoid it.

"JavaRanch, where the deer and the Certified play" - David O'Meara

posted 17 years ago

Anyone wanting to know more about shifting and bits might be helped by reading the campfire story here:

http://www.javaranch.com/campfire/StoryBits.jsp

http://www.javaranch.com/campfire/StoryBits.jsp