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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bit Shifting" Watch "Bit Shifting" New topic
Author

Bit Shifting

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

After reading this thread, I got to thinking (I know, I shouldn't do that...).
In my limited amount of years programming, I have never came accross a situation where I was like "I really need to shift these bytes to the left..."
So my question is what situtation might one need to do things like this? If this is too broad a question, I apalogize for my ignorance up front.


GenRocket - Experts at Building Test Data
Gary Peck
Greenhorn

Joined: Jun 03, 2003
Posts: 15
I think it is simply used to give test-makers a better chance at failing people.
Ive heard that it is used in ticker screens where the bits shift with the screen??
That's about the only real life situation I have ever heard...and...I ain't takin THAT job!!
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I have never had to shift bytes in any direction, and hopefully never will. I have no desire to be programming down there.


Please ignore post, I have no idea what I am talking about.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, the classic usage is packing multiple values into an int. I know this kind of stuff makes people go " ", but if you're going to have two million objects which each need ten state values each of which can have three different value, you can use ten integers (80 megabytes total) to store them, or one integer (8 megabytes total.)
We actually have a 3D graphics program which does exactly this with its polygons. The bit-packing was introduced to save memory in response to actual problem reports.
So we've got code that looks like

(A polygon is an int array, and one element is reserved for the flags. There used to be a Polygon class with an array member, but turning all the methods into statics that operated on just the array saved ~16bytes per polygon, or 32MB for a typical run.)


[Jess in Action][AskingGoodQuestions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks for the info.
Any why is it you are doing 3D programmin in JAVA Ernest?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
You might also check out this thread. While not as cool as 3D polygons, there's some code there for packing an [X,Y] grid coordinate reference into a single integer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


And why is it you are doing 3D programming in JAVA Ernest?

Why not? We do the transform and lighting in Java, and use OpenGL for the actual rendering. This is a 3D mesh viewer that's part of a finite-element analysis model-building/mesh editing tool. The reason we do T&L in Java is because there's also a software renderer so the code works on machines without GL, and we want a minimum of redundant code. We just feed the lit, transformed polygons to OpenGL to blit them on the screen. Because it's a mesh viewer, we want to see the polygons, so we don't want Gourad shading anyway.
This is my main job at Sandia, actually: writing software with which people build simulations to run on the world's largest supercomputers. It's plenty fun. Java and Swing's portability make our tools usable on a wide range of hardware, and we get great performance from advanced algorithms.
Actually, haven't we had some OpenGL/Java conversations before? Yep, here. It mentions that we use our own (my own, actually) native-code OpenGL wrapper.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Assembler hackers use bit shifting to do multiplication and division at lightning speed. But only by powers of 2.
Packing multiple values into an int sometimes happens when you work with a non-Java system. Maybe it feeds you numbers with the big and little bytes reversed or works natively with 3-byte numbers or something equally unsavory.
I have never had to use shifting in Java.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Why not? We do the transform and lighting in Java, and use OpenGL for the actual rendering.
Aha. That makes sense. I guess it depends on application. A friend of mine and I did a project with gl4java, found here, and as I said in that other post, gl4java needs some work, but has not been touched for 2 years. Anyway, that is way off topic. You should however give me access to the wrapper you wrote.
I understand what everyone is saying about shifting bits. And I thank you for the explinations. I guess my only comment would be, and don't kill me, an application where you are really that concerned about memory, maybe you should use a different language.
And non-pure java scenerios don't count.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Gregg Bolinger:
I understand what everyone is saying about shifting bits. And I thank you for the explinations. I guess my only comment would be, and don't kill me, an application where you are really that concerned about memory, maybe you should use a different language.

Sometimes it's a speed thing as well. In my stupid little example for instance, if you follow it back to the original programming diversions thread, you'll notice that object creation was a bottleneck. Performance was vastly increased by storing the information I had been keeping in objects in primitives instead. Bit-shifting facilitated this.
[ November 05, 2003: Message edited by: Jason Menard ]
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
One more thought on this topic:
Bit-shifting is frequently employed in computer-based cryptography.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Originally posted by Gary Peck:
I think it is simply used to give test-makers a better chance at failing people.
Ive heard that it is used in ticker screens where the bits shift with the screen??
That's about the only real life situation I have ever heard...and...I ain't takin THAT job!!

Bit shifting is used very often when dealing with hardware and machine control. Sensor information from limit switches, position sensors and the like are often packed into word size units.
The paper out sensor on your printer may be bit 4 of status word 3, so you have to use bit masking and/or shifting to see if you need to add paper.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bit Shifting
 
Similar Threads
want to Take SCJD
Mojarra X Java 6
How does Java access JavaScript Values
Javax.servlet.http.HttpServletRequest' is received as weblogic.servlet.internal.ServletRequestImpl
File not found