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.
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!!
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.)
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.
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
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.
Joined: Nov 09, 2000
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 ]
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com