wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Shift Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shift Operators" Watch "Shift Operators" New topic
Author

Shift Operators

Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
Can anyone tell me a practical reason for using shift operators? I've read the Cat and Mouse campfire story, and think I hava a good understanding of how they work.
Some of my java books don't go into them at all, while others give a definition and a few examples, but none give any real-world application. They seem pretty esoteric.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I haven't read the Cat and Mouse story, so I apologize if I repeat something.

In college, when I did c/c++ programming, we used bitwise logic and bitshifting extensively when we were writing programs that communicated with attached devices through a serial port. The kind of thing like: "If button A was pressed on the device, light up the odd numbered lights and turn on fan 4"

So bitshifting is used in control systems.

Another use of bitshifting (which I've never used but only read about) is to perform division and multiplication in powers of 2.

11110 is 30
30 / 2 = 15
15 is 11110 shifted once to the right. (1111)
(In Java, I think you should use >>> for mathematical bitshifting) It is a 'cheaper' operation for a processor to shift bits than it is to use registers to perform math. The gain is small when we have 1GHz systems, but I guess "back in the day", it might have meant the difference between minutes and 10's of minutes for mathematically intense operations.

Not sure if that helps
[This message has been edited by Mike Curwen (edited March 30, 2001).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

As Mike mentioned, bit shifting is sometimes used to speed up calculations of dividing and multiplying by powers of two. I have also used bit shifting in image manipulation... you can use bit shifting to rip RGB values out of colors, mess with them, and then construct a new color by shifting them back into place.
HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Brad Ford
Ranch Hand

Joined: Mar 22, 2001
Posts: 40
A bit off-topic for the forum, but on topic for the question - are there a lot of bitwise operator/function related questions on the SJCP exam? I've got a handle on the way they work but it takes me a while to work through problems with them. Sort of like translating into an unfamiliar language.
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
Thanks! Yes, that helps. I can't imagine there will be too many opportunities for me to use them. But who knows, maybe I'll get a job someday writing java code to control toasters.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Don't forget encryption and check sum algorythms CRC32 is basically calculated by & all the bits together to give a final 32 bit representation. Additionally, conversion between files saved in a different language such a C. If you get the speck on how MS saves an Icon file, you find that you have to use shifting to reconstruct the typedefs and the actual image is tw seperate bit maps. The first gets & over the area on the screen and the second get ^ over the top of that to give you the transparent region.

------------------
Hope This Helps
Carl Trusiak, SCJP2


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
wood burning stoves
 
subject: Shift Operators