Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

how to optimize the following code?

This topic is 2904 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

Advertisement
x * 2 == x << 1 //shifting by 1 yield the same as multiplying by 2
x * 4 = x * 2 * 2 = (x << 1)* 2 = ((x << 1) << 1) = x << 2

Share this post


Link to post
Share on other sites
what about this ?
int a = b % 1;

also following your logic

int a = (b + c) / 2;

should be

int a = (b + c) >> 1; ??

thanks

Share this post


Link to post
Share on other sites
Quote:
Doesn't the compiler do this optimization automatically?

It very well could. And You might want to leave it to the compiler to figure this out anyway, if only because it is a micro-optimization that clutters up your code.

int datacount = input << 2;
takes longer for a person to parse than
int datacount = input * 4;


Quote:
int a = b % 1;

Think logical bit operations aswell.

Again, it probably isn't worth optimizing till it is actually a problem.

Quote:
int a = (b + c) >> 1; ??

Yes.

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilNando
what about this ?
int a = b % 1;
The remainder when dividing by one?
Well that's simply a long way of writing:
int a = 0;

Share this post


Link to post
Share on other sites

int a = (b + c) / 2;
should be
int a = (b + c) >> 1; ??


yea, but remember, that the shift operator works on integral types, so b and
c has to be correctly defined.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!