[java] Bit Shifting`

Started by
4 comments, last by Wrathnut 23 years, 7 months ago
Here I go again with another newbie type post... I''ve been trying to streamline a few classes I''ve been working on and I''ve noticed that some people use bit shifting to increase the execution speed for some of their functions. Anyway my question is this: How much faster is bit shifting than regular multiplication and division... Also I''ve found a few places on the net that get into how bit shifting works but nothing that really explains it to my satisfaction. Does anyone know of a good tutorial on the subject? Any comments and all help would be much appreciated. War doesn't determine who is right, war determines who is left.
Advertisement
bit shifting is up to 4 times faster than multiplication and up to 7 times faster than division. however, shifting only works for multiples of 2.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Bit shifting is much faster than multiplication and especially division. The diffirence is not that huge anymore beacuse multiplication/division is much faster on modern processors than it was before. But still, in a tight loop one can get quite a boost by using bitshifts.

Good compliers suppose to optimize muls/divs into shifts for you but i am not sure that they actually do it.

Bitshifts olny work with integer numbers, not floats.

Here''s an example:
A/2 is same as A>>1
A/4 is same as A>>2
A/8 is same as A>>3
etc
A*2 is same as A<<1
A*16 is same as A<<4
etc

Here''s a tricky one (I am sure complier will not optimize it):
A*320 is same as (A*256) + (A*64) same as (A<<8) + (A<<6)

Cheers
Except for when I''m doing really wacky math sometimes, I notice no difference with my apps. It seems all the mults are being optimized into shifts anyway.
Yeah, i see some speed increases with bit shifting, but all these modern JITs and optimizers and compilers and what not usually change mutliplcation and division by powers of 2 into bit shift ops.

this is how it works:

2 * 4 = 8 (also) 2 * (2^2) = 8
0010 * 0100 = 1000
(you can''t really visualize the math
with binary, but that''s what 2,4,& 8
are in binary)

and
2 << 2 = 8
0010 (move everything right by 2) = 1000

Bit shifting is also usefull for dealing with numbers that don''t represent numbers.
(i had this really long explanation but it got too confusing)
Look up bit masking online, or in a book
Hey thanks for all of the great explanations guys, I think I've finally got a good handle on it... ^_^ Maybe I'm finally ready to start optimizing...

Jim_Ross: Most definitely, sounds kinda like some of the old tricks I used to pull on an Apple IIe, ehehe. You know it's pretty pitiful if you can fondly remember programming one of those pieces of crap.

War doesn't determine who is right, war determines who is left.

Edited by - Wrathnut on September 8, 2000 5:05:58 PM

This topic is closed to new replies.

Advertisement