Skip to main content
gamedev.net
🔒 Locked

Bitshifting question

Started by Red Ant 9 replies 500+ views
Original Post
Red Ant
Red Ant
Hi all, Just a quick question. If I want to divide a numer by say 4 I could do it the conventional way ( iNumber = iNumber / 4 ) or I could shift every bit of iNumber right two positions (iNumber = iNumber >> 2 . Are there any performance differences or is it just the same speedwise? Thanks.
Zeusel
Zeusel
as far as i know the bitshift is faster, i dont know how much, but you can think why! divide a number by 4(in mind) and then shift everything 2 places to the left, whats faster for you?
"be fair! ... always"Zeusel
Stoffel
Stoffel
If you''re talking integers and multipliers/divisors of powers of two, every compiler built in the last 20 years will convert /4 to >>2, and *4 to <<2. There is no performance difference.
GuyJohnston
GuyJohnston

Stoffel,

With that in mind I found this in gamedev resources:

http://www.gamedev.net/reference/programming/features/performance/page5.asp


quote:

Joris Timmermans

Logically, they are the same. For this simple example, the compiler might even turn the first into the second, but as you get more complex ( i = i << 8 + i << 4 is equivalent to i *= 272 for example )the compiler might not be able to make the conversion.



So is this in error, or is there an advantage using bit shift operators in complex equations.

Guy
Adulthood is the vehicle for making you childhood dreams come true.
Oluseyi
Oluseyi
As the quote says, the compiler can not ascertain complex deconstructions (how would the compiler "know" that x * 320 is the same as x << 8 + x << 6?) In such situations, the responsibility lies with the programmer.

On a related note, I once tried to find out whether the powers of 2 representation of any number could rapidly be "divined"; it ended up taking longer to figure out the summation of powers than it took to perform the original operations I was trying to optimize. (I was trying to see if I could convert arbitrary multiplications - a * b where both a and b are integer variables - into a << z + a << y + a << x + ... form)
UnshavenBastard
UnshavenBastard

Yeah, sure an interesting idea.
Ive tried this, too, some time ago,
but ended up like you said,
it gets too complex, and won
t be an optimization.
(I never finished that, gave up once I saw how it will end..)

But maybe it nearly doesnt matter these days,
if you multiply of shift. How much cycles does
an imul take on a P4, 1..2 cycles?
Dunno, but someday it sure won
t matter anymore...
Stoffel
Stoffel
GuyJohnston,
AFAIK, that post is on-target: compilers generally won''t break down a multiply into two shifts and an add. (This is, BTW, why I qualified my response with "for integer multiples of two".) Whether two shifts and an add is faster than an integer multiply, I''m not sure. I didn''t want to bring it up in my first response because these kinds of nitty-gritty optimizations shouldn''t take place unless the coder is sure that the code in question is creating a bottleneck.
KwamiMatrix
KwamiMatrix
what is a bitshift? thankx

Edem Attiogbe
Edem Attiogbe
doogle
doogle
A bit shift is where, as the name suggests, you shift each bit left or right. ie:

byte x = 0x1A //in binary 0001 1010 or in decimal 26
byte y = x << 2 // y now = 0110 1000, or just all the bits shifted two to the left.

i just learnt this myself, so please excuse me if this is wrong
KwamiMatrix
KwamiMatrix
I see. still taking it in. thankx doogle

Edem Attiogbe
Edem Attiogbe
doogle
doogle
theres a really good article on this site about bitwise operators, well worth a read.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.