Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Shift operations

Sagarika nair
Ranch Hand
Posts: 39
Hello friends!
This is the first time I am posting a question on this site,but ya I've been following up with all the useful information presented here since a few days.It has been extremely useful in my SCJP exam preparation.I am new to the subject and I find shift operators quite confusing.My level of convenience with shift operators has increased after I went through some of the topics on the forums.But if I could get some detailed information about shift operators I would consider it very helpful.For example the result of(-243>>-33) or something like (-256>>-33).How do we go about the solution to such a question on the exam?Any short cuts or any ideas for quicly solving such questions without wasting time(binary conversions and then shifting etc etc) on the exam?Thanks in advance.

Harwinder Bhatia
Ranch Hand
Posts: 150
Hi Sagarika
Welcome to the ranch.
I don't have links to online resources on this topic but this thread might help a little:
http://www.coderanch.com/t/243836/java-programmer-SCJP/certification
Cheers
Harwinder

Sagarika nair
Ranch Hand
Posts: 39
Thank you so much Harwinder.I went through the thread and it was extremely useful.I am clear about all that is explained.But it still doesn't show me an example of when the left operand is a negative number.That is what I am not very clear about.If possible could you please show me the working solution to such a problem.
Eg. -250<<-2
The result of the above operation is -2147483648.

Harwinder Bhatia
Ranch Hand
Posts: 150
Hello Sagarika
It doesn't matter whether the the operands (both left and right) are positive or negative, you just need to apply the basics
OK, let solve this problem: -250 << -2
Step I. Convert -250 into binary form:

Step II. Convert -2 into binary:

Step III. Apply the left shift operator:

So, don't get intimidated by the sign of the operands.
Hope that helps.
Cheers
Harwinder
[ November 20, 2003: Message edited by: Harwinder Bhatia ]

angel fire
Greenhorn
Posts: 2
I did not understand anything.
Can I use for example:
x=-64, y=-4
-64<<-4 ==> x * 2^y
because I allways use this for positive numbers.
tks

Aleksandar Stojanovic
Greenhorn
Posts: 26
Hello Angel Fire
I wouldn't say that you are wrong but there is an error and therefor I will try to explain you step by step, OK?
Let's start from the beginig
you said that you always use -64<<-4 ==> x * 2^y and on the end it results in -4, am I correct or misunderstood something???
but the correct result is 0.
How did i come to 0.
let's first resolve left side -64, way to tranform -64 into bynary represantation is first write 64
64 0000.0000.0000.0000.0000.0000.0100.0000
~64 1111.1111.1111.1111.1111.1111.1011.1111
-----------------------------------------
result 1111.1111.1111.1111.1111.1111.1100.0000 it represents -64

now let's resolve right side -4, will will do the same process as we done with -64.
4 0000.0000.0000.0000.0000.0000.0000.0100
~4 1111.1111.1111.1111.1111.1111.1111.1011
-------------------------------------------
res. 1111.1111.1111.1111.1111.1111.1111.1100 ok, its represent -4
but pay attension here, now you must take in consideration only five right-most bits ( 1.1100 and this is 28 OK ) (probably you ask why five ? because only with 5 bits you can represen range from 0 to 31 - range of shifting for int type or 6 bits in case of long) am i clear???
finally on the and what we have is

1111.1111.1111.1111.1111.1111.1100.0000 << 28 =
0000.0000.0000.0000.0000.0000.0000.0000 = 0
If i am wrong please correct me.

Dimple Kaushik
Ranch Hand
Posts: 49
Yah this shift operator thing is too confusing. Everytime i read a post i feel i understood everything but when i tried to solve some on my own i again end up with lots of confusion.
I wish one of the brainys from javaranch to write a good tutorial on this.
If I finished my exam with satisfacting score(otherwise i'm not confident) i would go for it

Harwinder Bhatia
Ranch Hand
Posts: 150
Angel and Dimple,
Since Sagarika said that she understood the other post, I assumed that she had reasonably good understanding of how shift operators work. Therefore, I didn't go into the nitty gritty details of each step.
I'm no expert in this subject. I was in the same situation (or even worse) with shift operators about a month ago as you guys probably are today. I completely agree that this stuff is confusing for a beginner and most books that I've read don't explain this topic in much detail.
Trust me there isn't much to it, if you follow the basic steps you can solve virtually any problem. If you ask very specific doubts, I would be happy to clarify them for you "in detail".
Harwinder

angel fire
Greenhorn
Posts: 2
tnks people. I understood the explain.

Sagarika nair
Ranch Hand
Posts: 39
Thanks once again Harwinder.Once again it seems to me that I understood what u have explained atleast the example you have presented.And I completely agree with dimple coz I feel like I've understood these shift operators at one point and then I get stuck up somewhere.