-
August 26th, 2009, 05:02 AM
#1
Arithmatic Shifting Vs division
Is it possible to do the division operation using left shift and right shift of bits when the divisor is not an exact power of 2
Thanks In Advance!
-
August 26th, 2009, 06:25 AM
#2
Re: Arithmatic Shifting Vs division
Originally Posted by Dave1024
Is it possible to do the division operation using left shift and right shift of bits when the divisor is not an exact power of 2
Thanks In Advance!
Yes.
As a matter of fact, it is how a standard division is done by hand.
http://courses.cs.vt.edu/~cs1104/BuildingBlocks/divide.030.html
On a side note, it is NOT something you want to do in real world code. Even if your divisor is 2, I wouldn't suggest shift division. It is not 100% portable and is error prone (what happens if your number is negative, or odd, or both? Or if it is a floating point type number?)
Use standard division and let c++, your compiler and your processor worry about that crap. They can optimize 10 this stuff ten times better than you ever will. You can never go wrong, and your code will be fully optimized AND error free.
Last edited by monarch_dodra; August 26th, 2009 at 06:27 AM.
Reason: Typo
-
August 26th, 2009, 11:03 AM
#3
Re: Arithmatic Shifting Vs division
I would bet that your compiler will do that for you when doing optimizations.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|