Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shift Operators

Joe McGuire
Ranch Hand
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
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
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

Ranch Hand
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
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
Posts: 3341
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