Thread: Finding next largest integer multiple

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Phoenix, AZ
    Posts
    5

    Finding next largest integer multiple

    Hi all. I recently stumbled across the following code:
    Code:
    do 
    {
         some_operation();
    } while (++val & 7);
    What this (supposedly??) does is continuously call some_operation and increment val until val is an integer multiple of 8. I understand binary and use of the bitwise-operator, I just can't convince myself that this is 'true' for and works properly for all integer m such where (m*8) < val < (m+1)*8 . Is this some kind of trick only possible because 8 is a power of 2? The formula clearly does not work if we do (++val & 4) until val is a multiple of 5, as this test will fail for decimal values 1, 2, and 3..

    Moreover, I need to expand this functionality for any integer n > 1, not just n = 8. Obviously the trivial solution is
    Code:
    do
    {
        some_operation();
    } while( (++val % m) != 0 );
    But I have had it burned into my head that the modulo operator is expensive. Is there a better, more efficient solution for this problem?
    Last edited by trinitrotoluene; 06-16-2010 at 05:09 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Is there a better, more efficient solution for this problem?
    Is there a better, more efficient solution for this problem?
    Maybe it is sufficient to compute ((val / n) + 1) * n, assuming non-negative integer division, and that you really do want the next largest multiple of n even if val is perfectly divisible by n.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Phoenix, AZ
    Posts
    5
    Ah, you mean something like:
    Code:
                    next = ((val/n)+1)*n;
                    do{
                            something();
                    }
                    while( ++val < next );
    Yes, that seems to work as desired, and is somewhat more clear and flexible than the original.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the largest and smallest of three integers
    By SilentPirate007 in forum C Programming
    Replies: 15
    Last Post: 03-21-2010, 01:25 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Finding values in a column that are multiple of 4
    By cjohnman in forum C Programming
    Replies: 9
    Last Post: 04-17-2008, 02:26 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM