• Boojum (unregistered)

    And for the paranoids who don't trust their processors addition instruction:

    unsigned short addEleven( ref unsigned short number )
    {
        unsigned short result = ( number & 1 ) ^ 1;
        unsigned short carry =  ( number & 1 ) << 1;
        result = result | ( ( number & 2 ) ^ carry ^ 2 );
        carry =  ( ( number & 2 ) | carry ) << 1;
        result = result | ( ( number & 4 ) ^ carry );
        carry =  ( number & 4 & carry ) << 1;
        result = result | ( ( number & 8 ) ^ carry ^ 8 );
        carry =  ( ( number & 8 ) | carry ) << 1 ;
        result = result | ( ( number & 16 ) ^ carry );
        carry =  ( number & 16 & carry ) << 1;
        result = result | ( ( number & 32 ) ^ carry );
        carry =  ( number & 32 & carry ) << 1;
        result = result | ( ( number & 64 ) ^ carry );
        carry =  ( number & 64 & carry ) << 1;
        result = result | ( ( number & 128 ) ^ carry );
        carry =  ( number & 128 & carry ) << 1;
        result = result | ( ( number & 256 ) ^ carry );
        carry =  ( number & 256 & carry ) << 1;
        result = result | ( ( number & 512 ) ^ carry );
        carry =  ( number & 512 & carry ) << 1;
        result = result | ( ( number & 1024 ) ^ carry );
        carry =  ( number & 1024 & carry ) << 1;
        result = result | ( ( number & 2048 ) ^ carry );
        carry =  ( number & 2048 & carry ) << 1;
        result = result | ( ( number & 4096 ) ^ carry );
        carry =  ( number & 4096 & carry ) << 1;
        result = result | ( ( number & 8192 ) ^ carry );
        carry =  ( number & 8192 & carry ) << 1;
        result = result | ( ( number & 16384 ) ^ carry );
        carry =  ( number & 16384 & carry ) << 1;
        number = result | ( ( number & 32768 ) ^ carry );
    }
    
  • (cs) in reply to DZ-Jay

    Seriously, if you are reading these posts and trying to determine which ones will effectively and correctly increment a number by 11,  or if you think you need to post a real "solution" to the problem, or if you are amazed that so many programmers don't know that i will always be less than i+11, then this might not be the best website for you.

  • (cs) in reply to diaphanein

    #include <assert.h>

    struct bestNumberEver {
      int value;
      bestNumberEver(const int& i) : value(i % 11) {}
      bestNumberEver(const bestNumberEver& n) : value(n.value) {}
      operator int() const {
        return value;
      }
      bestNumberEver operator+(const int& i) const {
        return bestNumberEver(value + i);
      }
    };

    int main() {
      bestNumberEver myNumber = 1;
      assert(myNumber == myNumber + 11);
      return 0;
    }

  • (cs) in reply to bro1
    i -= (++i - ++i) + (++i + ++i) - (++i + ++i);
  • (cs) in reply to Jake McArthur

    Anonymous:
    Many people here are making the same WTFs! Look at this one closely. i will never be equal to i+11!

    Ahem... ours are on purpose [:D] At least mine was.

  • (cs) in reply to Irrelevant

    Spinal Tap didn't want to increment by 11, that was the ultimate goal.  Perhaps someone should come up with a Base11 class for this person so they can just call a ++ operator on it to make their lives easier.

  • Betty (unregistered) in reply to Lenny

    int j = i + 11;
    while(i++ < j) {}

  • (cs) in reply to rogthefrog

    In any case, I'd do it like this, just in case you ever need to support decrements as well.

    <FONT face="Courier New" size=2>// IncrementorClass.java</FONT>

    <FONT face="Courier New" size=2>class OperationClassFactory
    {
      public static OperationClass getOperationClass(int number, int inc_val)
      {
        if (inc_val < 0)
          return new Decrementor(number, inc_val);
        else
          return new Incrementor(number, inc_val);
      }
    }
    interface OperationClass
    {
      public void performOperation();
      public int getResult();
    }</FONT>

    <FONT face="Courier New" size=2>class Incrementor implements OperationClass
    {
      private int number;
      private int inc_val;
      public Incrementor(int n, int times)
      {
        number = n;
        inc_val = times;
      }
      public void performOperation()
      {
        for (int i = 0; i < inc_val; ++i)
          ++number;
      }
      public int getResult()
      {
        return number;
      }
    }</FONT>

    <FONT face="Courier New" size=2>class Decrementor implements OperationClass
    {
      private int number;
      private int inc_val;
      public Decrementor(int n, int times)
      {
        number = n;
        inc_val = java.lang.Math.abs(times);
      }
      public void performOperation()
      {
        for (int i = 0; i < inc_val; ++i)
          --number;
      }
      public int getResult()
      {
        return number;
      }
    }</FONT>

    <FONT face="Courier New" size=2>public class IncrementorClass
    {
      public static void main(String[] args)
      {
        if (args.length != 2)
        {
          System.err.println("Usage: java IncrementorClass number number_to_add");
          System.exit(1);
        }
        OperationClass o = OperationClassFactory.getOperationClass(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
        o.performOperation();
        System.out.println(args[0] + " + " + args[1] + " = " + o.getResult());
      }
    }</FONT>

  • (cs)

    What about Duff's Device?

    int j = 11;
    switch (j % 4) {
    	/* Don't want to get here - die */
    	default: *(void *) 0;
    
    <b>while</b> (--j) {
    	<b>case</b> 0: ++i;
    	<b>case</b> 1: ++i;
    	<b>case</b> 2: ++i;
    	<b>case</b> 3: ++i;
    }
    

    }

  • Betty (unregistered) in reply to Betty

    seriously this board software is the real WTF

    while(i++ lessthan j) {}

    really not sure how to post a code block in this =)

  • Boojum (unregistered)

    And for the paranoid who don't trust their processors addition instruction:

    unsigned short add_eleven( ref unsigned short number )
    {
        unsigned short result = ( number & 1 ) ^ 1;
        unsigned short carry =  ( number & 1 ) < < 1;
        result = result | ( ( number & 2 ) ^ carry ^ 2 );
        carry =  ( ( number & 2 ) | carry ) < < 1;
        result = result | ( ( number & 4 ) ^ carry );
        carry =  ( number & 4 & carry ) < < 1;
        result = result | ( ( number & 8 ) ^ carry ^ 8 );
        carry =  ( ( number & 8 ) | carry ) < < 1 ;
        result = result | ( ( number & 16 ) ^ carry );
        carry =  ( number & 16 & carry ) < < 1;
        result = result | ( ( number & 32 ) ^ carry );
        carry =  ( number & 32 & carry ) < < 1;
        result = result | ( ( number & 64 ) ^ carry );
        carry =  ( number & 64 & carry ) < < 1;
        result = result | ( ( number & 128 ) ^ carry );
        carry =  ( number & 128 & carry ) < < 1;
        result = result | ( ( number & 256 ) ^ carry );
        carry =  ( number & 256 & carry ) < < 1;
        result = result | ( ( number & 512 ) ^ carry );
        carry =  ( number & 512 & carry ) < < 1;
        result = result | ( ( number & 1024 ) ^ carry );
        carry =  ( number & 1024 & carry ) < < 1;
        result = result | ( ( number & 2048 ) ^ carry );
        carry =  ( number & 2048 & carry ) < < 1;
        result = result | ( ( number & 4096 ) ^ carry );
        carry =  ( number & 4096 & carry ) < < 1;
        result = result | ( ( number & 8192 ) ^ carry );
        carry =  ( number & 8192 & carry ) < < 1;
        result = result | ( ( number & 16384 ) ^ carry );
        carry =  ( number & 16384 & carry ) < < 1;
        number = result | ( ( number & 32768 ) ^ carry );
    }
    

    (Space added to bitshifts to placate the forum software.)

  • (cs) in reply to Irrelevant

    let's get that right...

    int j = 11;
    switch (j % 4) {
    /* Don't want to get here - die */
    default: *(void *) 0;

    while (j -= j-- - --j, --j) {
    	case 0: ++i;
    	case 3: ++i;
    	case 2: ++i;
    	case 1: ++i;
    }
    

    }

    It'll still break for when 11 <= 0, but you can't win them all.

  • jake (unregistered)

    I think you ALL have missed the answer.  It is obviously:

    i += 3; // Increment i by 11.


  • (cs)

    The next line from Steve should be:

    Nathan, I want you to repeat a very valuable phrase that I think will important for your career development, "Would you like fries with that?"

  • (cs) in reply to rogthefrog
    rogthefrog:

    loneprogrammer:
    // increment by eleven
    while(i != i + 11) {
       i++;
    }

    Save trees! Use less space!

    while(i++ < i + 11) {}

    [6]



    The < version will eventually terminate when i overflows.  !=
  • (cs)

    You are all missing the simplest solution.

    int UnsignedIntegers [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 .... };

    int incrementBy(int val, int amount) {
    return UnsignedIntegers[val+amount];
    }

    int result = incrementBy(i, 11);

    Simple! Now, I know there's a bit of work still to do to define the full range of the UnsignedIntegers array, but a quick Perl script can be used to generate it automatically.

  • (cs) in reply to Mike R
    Even without the forum software mangling the post this is my favorite.

    FYI this is how it really reads:


    while (i < (i+11)) { i++; }



    And now we need to put it in a function and name it aptly:

    int monkeyThatSitsOnABicycleWithABananaHangingBeforeHim(int i)
    {
      while (i < (i+11)) { i++; }
      return i;
    }

    Feel free to use this code in C, C++, Java or C# - good code is PORTABLE!

    Here's my own version:
    i /= 11;
    i++;
    i *= 11;

    I'm not responsible for any loss of precision you might encounter. Get better integers.
  • [email protected] (unregistered)

    No one has done it via bit-shifting and modulo operators, which is what I would do if I had the time/masochism.

  • Chris Brien (unregistered) in reply to [email protected]

    typedef int j;

    typedef struct i { j i; } i;

    j incrementByEleven(j I) { j j; i i = { .i = I };

    struct i k = i;
    
    printf("%d + 11 = ", i);
    
    for (j = 0; j < 11; j ++)
    {
    	switch (j)
    	{
    		case 10:
    			i.i = k.i + j;
    		default:
    			i.i ++;
    	}
    }
    
    printf("%d\n", i);
    

    }

    j main(void) { incrementByEleven(0); incrementByEleven(3); incrementByEleven(4); }

  • Grossly Panda (unregistered) in reply to Chris Brien

    I believe I've found the best solution. Not only that, it can EASILY be changed to increment by all kinds of other numbers, such as 11 and 13!

    public class IncrementBy12
    {
       public int increment(int in)
       {
          int[] numborares = {in, 12};
          return add(numborares);
       }
       
       private int add(int[] values) 
       {
          int negativeNumbers = 0;
          int result = 0;
          for (int i = 0; i < values.length; i++)
          {
             if (values[i] >> 31 == -1) 
             {
                negativeNumbers += Math.pow(2, i);
                result -= 1;
             }
          }
          int count = -1;
          while (++count < 31)
          {
             for (int i = 0; i < values.length; i++)
             {
                if ( ( (values[i] & 1)== 1) && ((negativeNumbers & (int)Math.pow(2, i)) != Math.pow(2, i)) ) result += 1 << count;
                else if ( ( (values[i] & 1)!= 1) && ((negativeNumbers & (int)Math.pow(2, i)) == Math.pow(2, i)) ) result -= 1 << count;
                values[i] = values[i] >> 1;
             }
          }
          return result;
       }
    }
    
    
  • Grossly Panda (unregistered) in reply to Grossly Panda

    Wow, the code-tag on this board is a sexy little wtf in its own right.

  • Sven2 (unregistered)

    This should work for unsigned numbers (spacing intended ;)):

    while (j>=i  +  11<=!j ) ++j;
    i^=j^=i^=j;

  • Sven2 (unregistered)

    Tricky code box o_O


    while (j>=i  +  11< =!j ) ++j;

    i^=j^=i^=j;

  • VincentLascaux (unregistered) in reply to Sven2

    This works (not tested though) only on chars (8 bit integers)

    i
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    ----------------------------------------------------------------------;

    If anyone want to write the code for 16 bit integers... it can be quite usefull on a processor where -- is significantly faster than ++ !

  • (cs) in reply to VincentLascaux

    You are all very, very silly people.

    \ Forth version (from memory, untested - caveat programmor)

    : INCREMENTER:
      CREATE  ( n -- | name )
        ,
      DOES> ( var -- ; increments var by n )
        @ SWAP +!  ;

    11 INCREMENTER: NATHAN

    0 VARIABLE i
    i NATHAN
    .( i was 0, is now ) i ?

  • Too embarassed (unregistered)
    <?php<BR><FONT size=4>$i = $_GET['i'];
    mysql_query('CREATE TABLE ii (i INT)');
    mysql_query('INSERT INTO ii VALUES (' . $i . ')');
    $i = mysql_result(mysql_query('SELECT i+11 FROM ii WHERE i=' . $i), 0, 0);
    ?></FONT>
  • Too embarassed (unregistered) in reply to Too embarassed

    Forgot to mention: the previous example is your typical PHP/MySQL job.

  • Darrin (unregistered)

    I can't believe that not a single person has posted an answer that requires the C preprocessor to do something magi-tragic.

  • (cs)

    10++ ?

  • anon (unregistered)

    #define PLUS +
    #define INCREMENTBY +=
    #define ONE 1
    #define TEN 10
    #define ELEVEN TEN PLUS ONE

    int X = 0;
    X INCREMENTBY ELEVEN;


  • Joakim (unregistered)

    In BLOCKED SCRIPT

    String.prototype.inc=function(by) {
      if(by>0) {
        var num = parseInt(this);
        by--;
        return (""+num).inc(by);
      }
    }
    
    function wtf() {
      alert("WTF: " + "0".inc(11));
    }
    
  • Tim (unregistered)

    Not such a WTF really, in Delphi / Pascal it would be...

    inc(i,11);

     

  • (cs) in reply to Tim

    Personally I prefer

    i -= -11

    or

    #define IPLUSELEVEN (i+11)
    i = IPLUSELEVEN

    Drak

  • no (unregistered) in reply to Drak

    // +>+++++[-<++>]
    int main() {
      unsigned char a[30000]
      int p = 30000;
      while(--p)
        a[p] = 0;
      a[p]++;
      p++;

      a[p]++;
      a[p]++;
      a[p]++;
      a[p]++;
      a[p]++;
      while(a[p]) {
        a[p]--;
        p--;
        a[p]++;
        a[p]++;
        p++;
      }
      a[p]++;
    }

  • exodus (unregistered) in reply to Drak

    <font size="3">function incrementBy11( &$i )
    {
        $result = mysql_query( "SELECT $i + 11" );
        list( $i ) = $row = mysql_fetch_row( $result );
        mysql_free_result();
    }

    $i = 123;
    incrementBy11( $i );
    </font>

  • exodus (unregistered) in reply to exodus

    oops....
    list( $i ) = $row = ...
    should be
    list( $i ) = ...

  • (cs)

    int i = 0;
    (((((((((((i++)++)++)++)++)++)++)++)++)++)++);

    off course :-)

  • Idiot (unregistered) in reply to macman

    Anonymous:

    j +=11;

    but what do I know, I just been doing this stuff since Moses. - Mac

    Good thing that you showed us all how to do it.. all of us here would have used the for loop...

  • Marya (unregistered) in reply to Idiot

    Why not shift this task to the user ? If it is too complicated for the programmer...

    Console.WriteLine("Curent i value is {0}. Please enter the value of i incremented by eleven:", i.ToString());
    i = Int32.Parse(Console.ReadLine());

  • Wessam Zeidan (unregistered)

    Guys, you forgot exception handeling, what if an exception is raised when you increment by 11 [:P]

  • (cs) in reply to Wessam Zeidan

    You can do it in a loop with just 4 iterations like this:-

    void add11(int &i)
    {
        int k,j=0,z=0,m=0;
        while (j<4)
        {
            if (j!=2)
            {
                k=i&m;
                i=(i>>z);
                i++;
                i=(i<<z);<z )=""><z )|k="">
            }
            z++;
            m=m<<1;
            m++;
            j++;
        }
    }

    </z></z>

  • (cs) in reply to foilman

    Replace i=(i<<z) above with i=(i<<z)|k;

    It got stripped out of the post for some reason...

  • MLB (unregistered) in reply to Pedant

    You need to use Ook!.net it is much more suitable for this problem

    Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook?  Ook. Ook? Ook.

    Ook? Ook.

  • (cs) in reply to endothermal

    in C.


    int addNumber(int i, int n)
    {
     int ans;

     if (n==0)
      ans = i;
     else
      ans = addNumber(i+1, n-1);

     return ans;
    }

    int addEleven(int i)
    {
     int ans;

     ans = addNumber(i, 11);

     return ans;
    }

    int main(int argc, char* argv[])
    {
     int i;

     i = 1;

     i = addEleven(i);

     printf("I=%d\n", i);

     return 0;
    }

     

    gives all sorts of opportunities for "addTen" "addOnehundred"... :-)

  • Chris (unregistered) in reply to fatgeekuk

    You are all great programmers... I bet most of you work on the Linux kernel.

    Why don't you just write:

    i += 11;

    Sometimes, the answer is that simple!

  • (cs) in reply to fatgeekuk
    fatgeekuk:
    ans = addNumber(i+1, n-1);

    May I suggest an optimisation: ans = addNumber(++i, --n);

  • (cs) in reply to Bellinghman

    Jeez - this forum software is so badly broken it's unbelievable.

    Alex - please, please, PLEASE get something better in.

    Pretty please with a side order of please.

  • (cs) in reply to Bellinghman

    This place looks like an open-ended festival of ways to add 11 to i.

    I can't resist chipping in [:p]:

    const ELEVEN = 11

    i = i + ELEVEN

  • (cs) in reply to Mike R

    Mike R:
    At which point does Natan realise he's asked a stupid question?

    Surprisingly, no, the question was asked entirely in earnest. Bless him. And we were too jaw-droppingly amazed to make it clear to him that it was a stupid question... [:S]

    Mike R:

     How can people like this sleep at night calling themselves programmers?

    I'm sure he does some of his best thinking when he's asleep. [6]

  • whatever (unregistered) in reply to Pedant

    int i = 1;
    while (true != false)
        i = i + 11;

Leave a comment on “If ++ Increments ...”

Log In or post as a guest

Replying to comment #:

« Return to Article