Jump to content
• Advertisement

#### Archived

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

# How fast is a /2 ?

This topic is 6914 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 guess the topic says it all, so can any of you gurus tell me how fast a divide 2 is? If it''s very slow how else can I get the center of a sprite efficiently and quickly? -------------------------
-Now Working on Pokemon like Engine!

#### Share this post

##### Share on other sites
Advertisement
Well you can do a shift by 1 instead of dividing by 2 and it does the same thing.

ex:
int a=4;
a = a>>1; //a now equals 2
this is extremely fast since it just shifts all of the bits over by 1. Note: you can also multiply by 2 this way as well a = a<<1;

Hope this helps

--FUEL

Edited by - fuel on May 19, 2000 2:27:55 AM

#### Share this post

##### Share on other sites
I''m no expert, but I do know that division is just repeated subtractions and multiplication is just repeated addition. However, the fastest way to do math for powers of two is by bitshifting (moving the bits around).

shift left = << <- that''s an operator, just like the cout operator
shift right = >> <- same thing, but like the cin operator. Just remember which way they point, that''s the way they''ll shift the bits.

The number 3 looks like this in binary:
0011

If we multiply it by two (or left shift one spot) we get:
3 * 2 = 3 + 3 = 0110 = 6
3<<1 = 0011<<1 = 0110 = 6 <- note that it shifted it 1 spot

If we divide it by two (or right shift one spot) we get:
0001 = 1 (integer math)
3 / 2 = while ((3 - 2) > 0) i++; i = 1
3>>1 = 0011>>1 = 0001 = 1

The bitshifting is much Much faster.

Quick reference here. This is the values for bitshifting. It''s just binary math, something you should know if you''re a programmer

<<1 == *2
<<2 == *4
<<3 == *8
etc...

>>1 == /2
>>2 == /4
>>3 == /8

32>>2 == 32 / 4 == 8
10000>>2 == 32 / 4 == 8

32>>3 == 32 / 8 == 4
10000>>3 == 00010 == 4

That was way too long winded... it''s actually very simple, I think I just explained it wrong :p Maybe someone else will have responded by now and mine''ll just be blown over

random

---
Wait, you mean I have to actually... THINK?!

#### Share this post

##### Share on other sites
Hi,

you did not tell which language you use.
A divide by 2 can quickly be done like this (c code):

a = a>>1;

It''s basicly a bitshift to the right which means a divide
by 2 and doesn''t cost very much...

Kind regards,
Metron

#### Share this post

##### Share on other sites
well, most modern compilers automatically convert division and multiplication by powers of two (and sums of powers of two) into bit shifts, so it doesn''t really make any difference anymore if you shift or divide, and it''s easier to read your code if you just divide.

--
Float like a butterfly, bite like a crocodile.

#### Share this post

##### Share on other sites
I''m using VB... heeeheh... not sure if I can bit shift there... well one way would be to only calculate it once... oh well thanx for the input.

-------------------------
-Now Working on Pokemon like Engine!

#### Share this post

##### Share on other sites
I think shifting is a function, not an operator, in BASIC, so it might not be faster.... but I don''t really specialize in BASIC-derived languages.

lntakitopi@aol.com | http://geocities.com/guanajam/

#### Share this post

##### Share on other sites
There is no function to shift bits on VB. You can use the integer division operator \ that is much faster than the float one...

André Luiz Silva
"There is no knowledge that is not power"

#### Share this post

##### Share on other sites
If you''re using floats, why not multiply with 0.5F

#### Share this post

##### Share on other sites
If the value you are trying to get can be obtained only once, and the used inside a cicle, by all means, do it. if the cycle is long (say filling a screen) whatever gain you get from using shifting or other improvements at obtaining the value can be gained from avoiding to repeat the same operation over and over again. (wicht isn''t to say that you shouldn''t try to speed up that operation, anyway.) A basic rule of optimising is to focus most on operations that occur repeatedly, since any gain you get on those will be multiplyed by how many times they get executed on average.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 10
• 33
• 29
• 9
• 15
• Advertisement
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!