Advertisement

<<>> or /* ??

Started by April 04, 2000 01:36 PM
15 comments, last by shpook 24 years, 8 months ago
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
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!
Advertisement
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.
A polar bear is a rectangular bear after a coordinate transform.
And while on the subject; how do u use bitshift? (in DJGPP for example)











========================
Game project(s):
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
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!
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?
Advertisement
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!"

George. F"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!"
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
=======================Game project(s):www.fiend.cjb.net
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.
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

This topic is closed to new replies.

Advertisement