Advertisement

How fast is a /2 ?

Started by May 19, 2000 01:11 AM
10 comments, last by Marauderz 24 years, 7 months ago
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!
-------------------------
-Now Working on Pokemon like Engine!
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
Founder Caffeinated Gameswww.caffeinatedgames.com
Advertisement
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?!
---Wait, you mean I have to actually... THINK?!
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
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.

--Float like a butterfly, bite like a crocodile.
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!
-------------------------
-Now Working on Pokemon like Engine!
Advertisement
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/
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"
"- To begin with, said the Cat, a dog's not mad. You grant that? - I suppose so, said Alice. - Well, then, - the Cat went on - you see, a dog growls when it's angry, and wags its tail when it's pleased. Now I growl when I'm pleased, and wag my tail when I'm angry. Therefore I'm mad."
If you''re using floats, why not multiply with 0.5F
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.

This topic is closed to new replies.

Advertisement