This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bit Shifting

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gary Peck
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never had to shift bytes in any direction, and hopefully never will. I have no desire to be programming down there.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info.
Any why is it you are doing 3D programmin in JAVA Ernest?
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thought on this topic:
Bit-shifting is frequently employed in computer-based cryptography.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic