CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    163

    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!

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Arithmatic Shifting Vs division

    Quote Originally Posted by Dave1024 View Post
    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

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    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
  •  





Click Here to Expand Forum to Full Width

Featured