• Post Reply Bookmark Topic Watch Topic
  • New Topic

Shift Operators  RSS feed

 
cj.lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying for my SCJP exam and I am wondering whether it is vital to know the Shift operators.
Please could anyone clarify this?
Thanks
CJ
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
You won't die if you don't know the shift operators. It's not even likely that you will fail the certification if you don't. But if you're aiming at becoming a decent programmer I would suggest at least learning the basics about the shifters, since they may help you out in times of need and despair. They're great to have as part of your programmer's toolkit, and the more you use them, the more use you will find for them!
//Kaspar
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cj.lee:
I am studying for my SCJP exam and I am wondering whether it is vital to know the Shift operators.
Please could anyone clarify this?
Thanks
CJ

The answer is yes. There won't be many question on Shift but at least one or two. It is on the Objective so learn it! Shift operatiors are esay enough. Good luck
 
cj.lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On this issue, does anyone know how to calculate the value using the shift operator >>> ?
For example,
12 >> 2 is equivalent to 12/2^2
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some infos on the following link:
http://www.javaranch.com/campfire/StoryBits.jsp or youmay also search for "shift operator" in this forum.
We also would like you to read the Javaranch Naming Policy and register again. Thank you for your cooperation.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mike Beaty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute
12>>2 = 3
and
12/2^2 = 4
Right?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
12>>2:
12 => 00000000 00000000 00000000 00001100
12>>2 => 00000000 00000000 00000000 00000011 = 3
12/2^2:
the precedence is as follows 12/(2^2) which amounts to 12/4 and equals 3. How do you come to 4?
Be aware that ^ is not the power operator in Java but the XOR operator which is kinda not the same.
Where is the problem ?
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 19, 2001).]

Edited by satya
ps:
12>>2 => 00000000 00000000 00000000 00000010
12>>2 => 00000000 00000000 00000000 00000011
Hope you don't mind.
- satya

[This message has been edited by Madhav Lakkapragada (edited December 19, 2001).]
 
Mike Beaty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but
2^2 = 0
0010
0010
----
0000
right?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Okay 2^2 wasn't 2 OR 2.
I think they were taking it as 2 to the power 2. = 4
regds.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That doesn't mean to say they are right.....dodge this
13>>2 is NOT the same as 13/2^2 (2 to the power of 2).
Anyone has other opinions......
- satya
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya,
that's what I have written, haven't I?
Moreover, 13>>2 equals 13/(2^2) since an integer division is performed or am I completely nuts here ?
Mike,
what cj.lee wnated to point out with 2^2 is 2 to the power 2 and not 2 XOR 2... Have a look at JLS 15.19 Shift Operators.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 19, 2001).]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Valentin:
Yup, you wrote the same, but looks like Mike din't catch it.
SO I repeated it (actually I said OR it should be XOR, I think).
Din't mean to step of your toes.....
Also I was thinking too many things so I din't observe.
I wanted to edit your post the 12>>2.
Then wanted to post the 13>>2 stuff.
Guess I should slow down....you see its the holidays and
we have so much sugar around the work place. Rice puddings,
Cinnamon pie's, Biscotti....and ofcourse sugar in the coffee...
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Moreover, 13>>2 equals 13/(2^2) since an integer division is performed or am I completely nuts here

I would like to say you are nuts... ...but
Professionally speaking, you are correct.
However, I personally wouldn't prefer to do
it that way ie; 12/2^2.
I prefer the bit shift method.
Give me a little time will you and I will prove why....
Stay tuned.
- satya
Edited:
Here we go perfect example of why I don't prefer that.......

I will leave the working for those interested.
Oh well,
D:\>javac Test001.java
D:\>java Test001
-127 right bit shift by 2 = -32
-127 divided by 4 ie; 2 square = -31
Happy, I know I am.
- satya


[This message has been edited by Madhav Lakkapragada (edited December 19, 2001).]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Din't mean to step of your toes
Actually I don't mind
Guess I should slow down....you see its the holidays and we have so much sugar around the work place. Rice puddings, Cinnamon pie's, Biscotti....and ofcourse sugar in the coffee.
you lucky guys
I would like to say you are nuts... ...but Professionally speaking, you are correct.

However, I personally wouldn't prefer to do it that way ie; 12/2^2. I prefer the bit shift method.
I agree with you I prefer the shift too, although the question was not which method to prefer...
Well, I think we stepped on each other's toes
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

or am I completely nuts here......
although the question was not which method to prefer

I am dying to see what you say abt my example....
- satya
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already answered that, look one post above your last one.
I say that, as you do, I prefer the shift method over of the division...
There's no place for fight here dammit
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
CJ Lee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to have caused problems with my notification.
I am glad that we managed to get a discussion on this issue.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There's no place for fight here dammit

yeah....yeah....yeah!

good discussion though, don't you think.
- satya

[This message has been edited by Madhav Lakkapragada (edited December 19, 2001).]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure is, Satya.
Me being nuts or not, I love to discuss anyway
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!