Jump to content

  • Log In with Google      Sign In   
  • Create Account

[java] Bit Shifting`


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Wrathnut   Members   -  Reputation: 377

Like
Likes
Like

Posted 08 September 2000 - 04:53 AM

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.

Sponsor:

#2 jenova   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 September 2000 - 04:58 AM

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.

#3 Meduzza   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 September 2000 - 05:05 AM

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


#4 Buster   Members   -  Reputation: 100

Like
Likes
Like

Posted 08 September 2000 - 05:26 AM

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.

#5 Jim_Ross   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 September 2000 - 07:39 AM

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

#6 Wrathnut   Members   -  Reputation: 377

Like
Likes
Like

Posted 08 September 2000 - 10:02 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS