Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

shpook

<<>> or /* ??

This topic is 6767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I may be asking this on the wrong board... if I am, sorry. I am using VC++ 6.0 as a dev environment and was told quite recently that using bit-shifting in place of multiplication and division was "obsolete" in a manner of speaking, because VC++ 6.0 ships with a built in incredibly efficient optimizer that renders such "tricks" useless. So I don''t have to do any research of my own , I was wondering if anyone could verify this. Anyone? Anyone? =) Thanks, -shpook

Share this post


Link to post
Share on other sites
Advertisement
Most modern compilers (including VC++) will optimize multiplication and division by powers of two as simple bit shifts. For that reason they are pretty much obsolete in terms of optimization, but there are still other things they are useful for. For example, if you are shifting bits of a color value to the right position to compose an RGB value, then it is much clearer to use the shift operators than to use mulitplication (IMHO) so they are not completely obsolete.

Check out the GPI project today!

Share this post


Link to post
Share on other sites
An optimization that I''ve seen more than once is the conversion from i * 640 to i * 512 + i * 128, which can be bitshifted nicely. Would the compiler optimize this as well (from 640) or should you still write it in the second form for the compiler to recognize it as a candidate for optimization?

A polar bear is a rectangular bear after a coordinate transform.

Share this post


Link to post
Share on other sites
For JonatanHedborg: if you plan one multiplying or dividing by a number that is a multiple of 2 (2, 4, 8, 16, 32, etc.), you can use bitshift to optimize the operation. for multiplication, use <<, and use >> for division.
For example, 3 << 4 would be like:
3 * (2^4)
hope this helps!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, compilers optimizing code is very frequent in this day and age. VC++ probably does convert them to bitshifts anyway, but bitshifting instead of multiplication can''t hurt, especially considering not everyone use VC++ or Borland, etc. If you feel pretty confident that you can read your code with bitshifting and all (or if you don''t you can comment on what you did) then by all means, bitshift.

Ok now for the question on What is bitshifting.

Bitshifting (in C) is the << operator. It shifts one bit or on/off value one place over. Say you had binary 0010 or 2 in decimal, bitshift would move it 0100 or make it four. Thats only a left shift, A right shift would do the opposite, making it one.

variable << how_many_times_to_shift; /* In C */
(variable << how_many_times_to_shift); // In C++

Get it?

Share this post


Link to post
Share on other sites
I agree with Anon. and I usually use bitshift.

I''m intrested in what Amitage was asking (if anyone knows...), but if you wanted to use that optimisation and you didn''t know if the compiler would do it, you could use the bitshifts yourself of course.

George.

"Who says computer games affect kids, imagine if PacMan affected us as kids, we'd all sit around in a darkened room munching pills and listening to repetitive music....uh oh!"

Share this post


Link to post
Share on other sites
is this right?

var2=1

var1=var2<<1

and at the end, var1 would be 2. right?

how do u use it to set and read flags?


========================
Game project(s):
www.fiend.cjb.net

Share this post


Link to post
Share on other sites
Well, why not try it - run your code in debug mode and view the disassembly. Go to the relevant line and look for something that looks like a shift rather than a multiply. I expect that it will do a fairly good job of optimising many multiplications by constants to shifts. Although whether VC will do this in the cheaper versions of the compiler, I don''t know.

Share this post


Link to post
Share on other sites
You don''t really need bit shifts for flags.

if( flag & 0x0080 )

The & operator would do the trick, since if consideres anything other than 0 true.

OneEyeLessThanNone - for some reason i dont have to put in the pass every now and then

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!