• (cs) in reply to Drak

    You fools. VB is obviously the best solution to this problem.

    function add11toI(string i) as string
    Dim i as string, x as string, x1 as string, x2 as string

    for z = 1 to -12.75 step -1.25
    x=right(i,1)
    if x="9" then
    t=2
    x1=left(right(i,2),1)
    if x1="9" then
    t=3
    x2=left(right(i,3),1)
    x2=right(cstr(cint(x2)+1),1)
    end if
    x1=right(cstr(cint(x1)+1),1)
    end if
    x=right(cstr(cint(x)+1),1)
    i=left(i,len(i)-t) & x2 & x1 & x
    next
    add11toI=i
    end function

    It's the prettiest method I've ever written. =D I don't think anyone could write a better performing method if they tried. May or may not compile. No one'll ever need more than 3 digits swapped anyway.

  • Anonymous (unregistered) in reply to Steve Local

    (First time posting here)

    Surely the answer should be


    double X = I, Y = 11.0;


    for (;;) {X += Y; Y /= 2.0;};

    I = X; // should never get here...


    And try to get as close to I + 11 as possible

  • Anonymous (unregistered) in reply to Anonymous
    Anonymous:
    (First time posting here)

    Surely the answer should be


    double X = I, Y = 11.0;


    for (;;) {X += Y; Y /= 2.0;};

    I = X; // should never get here...


    And try to get as close to I + 11 as possible


    Sorry, should be Y = 5.5 initially, duh.
  • jim (unregistered) in reply to Anonymous

    Couldn't resist. I expect the formatting will be screwed when I post this though.

        i++;i--;i++;i--;i++;
      i++;                i++;
    i++;     i--;  i--;     i++;
    i++;     i--;  i--;     i++;
    i++;                    i++;
    i++;        i--;        i++;
    i++;                    i++;
    i++;  i--;        i--;  i++;
    i++;      i--;i--;      i++;
      i++;                i++;
        i++;i--;i++;i--;i++;
    
  • (cs) in reply to Steve Local

    "Surprisingly, no, the question was asked entirely in earnest"

    Actually, it is a reasonable question in some circumstances.

    This is Nathan. He's a relative novice. He's been instructed on both ++ and +=. But he's been out in the wide world for a year or two, and though ++ is everywhere, he may never have actually seen += used in anger.

    Now, he's found a use for it. He knows what he wants to use, it's that nifty increment-like operator that allows you to do something a little more general than just add 1. But he can't remember exactly how to spell it, and the help file index is just confusing.

    So he innocently asks this question.

    On the other hand, maybe he is just dumb.

  • Jasmine Strong (unregistered) in reply to Pedant

    Oldschool ARM hackers in the house.

    incbyeleven:
        STMFA   r13!, {r0-r1, r14 } ; save some work registers, return address
        ADR r0, token_i         ; get the address of static int i
        LDR r1, [r0]               ; load it into r1
        ADD r1, r1, #11         ; r1 += 11;
        STR r1, [r0]                ; back it goes
        LDMFA   r13!, { r0-r1, r15 }  ; return

  • Sten (unregistered) in reply to lamecoder

    Nice "Spinal Tap" content.  Well done!

    Fun,

    Sten

  • Zepi (unregistered)

    Why not do it as a recursion?

    void AddBy11(int &i)
    {
    static int inc=0;
    if (inc++ < 11)
      add(i++);
    inc--;
    }
    

    Actually I'am not sure if that static will work correctly in recursion.. At least it should.

  • (cs)
    int target = i + 14;

    while(i != target)
    {
      i = rand();
    }

    //Solutions analogous to the above are not rare.

  • (cs) in reply to no
    Anonymous:
    // +>+++++[-<++>]
    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]++;
    }



    Brainfuck.
    <3

  • (cs) in reply to dhromed

    Strings!


    var eleven = 'doaddeleven';

    i += eleven.length;

  • None (unregistered) in reply to MLB
    Anonymous:
    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.


    This is equivalent to:

    i -= 11;

    Because "Ook? Ook." is equivalent to Brainf***'s "-" operator. "Ook. Ook?" is equivalent to Brainf***'s "+" operator.

    For sports, here is the corrected Ook! implementation:

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

    And while we're at the Brainf*** derivates, here is the f*ckf*ck variant:

    f**k f**k f**k f**k f**k f**k f**k f**k f**k f**k f**k

    :-) Blimey, I love Brainf***
  • (cs) in reply to None

    omg, you people have wayyy to much time on your hands..

    Don't you all realise its easier to send a query to the DB?

    sprintf("SELECT (%d + 11) FROM DUAL", i);

    tsk tsk... you all need to learn how to do it RIGHT!

  • Chris Brien (unregistered) in reply to Kahei

    I propose using the "Bogoadd" function. It's similar in concept to Bogosort ( http://www.catb.org/~esr/jargon/html/B/bogo-sort.html ), which, as well know, can sort an arbitrary-length list in constant time.

    int bogoAdd(int l, int r) { int result = QuantumRandomnessFactory.getQuantumRandomNumberSource().getQuantumRandomInteger();

    if (result != (l + r))
    	destroyUniverse(Universe.getCurrentUniverse());
    
    return result;
    

    }

    int addEleven(int i) { return bogoAdd(i, 11); }

    The implementation of the destroyUniverse() function is left as an exercise for the reader.

  • Anonymous (unregistered) in reply to Richard
    Anonymous:
    ++++++++++++i++++++++++++++;


    But ++ as a postfix operator returns the previous value, which can't be a reference, so i++++ should not work?

    As you see, this question looks rather easy. But if you obvserve the solutions proposed by coding experts here, and consider that half of them don't even work, it seems like a valid question to me!
  • (cs)

    Use a web service:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;
    using System.Threading;

    namespace DailyWTF
    {
        interface IElevenIncrementer
        {
            int AddEleven(int number);
        }

        class ElevenIncrementer :
            MarshalByRefObject,
            IElevenIncrementer
        {
            #region IElevenIncrementer Members

            public int AddEleven(int number)
            {
                return number + 11;
            }

            #endregion
        }



        class Class1
        {
            [STAThread]
            static void Main(string[] args)
            {
                ElevenIncrementer incrementer = new ElevenIncrementer();

                ChannelServices.RegisterChannel(new HttpChannel(1111));
                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(IElevenIncrementer),
                    "ElevenIncrementer",
                    WellKnownObjectMode.SingleCall);
                
                Thread.Sleep(Timeout.Infinite);
            }
        }
    }

     

  • (cs) in reply to antareus

    I forgot to add: the web service is optimal because if the definition of "i plus eleven" ever changes all we have to do is change the web service and the end-user experience is unaffected.

  • spotcatbug (unregistered)

    In C:


    void addEleven(unsigned int *a)
    {
        unsigned int    b = 1, c = 0;

        while (b != 0)
        {
            if ((b & 1) || (b & 2) || (b & 8))
            {
                if (*a & b)
                {
                    if (!c)
                    {
                        *a &= ~b;
                        c = 1;
                    }
                }
                else
                {
                    if (!c)
                        *a |= b;
                }
            }
            else
            {
                if (*a & b)
                {
                    if (c)
                        *a &= ~b;
                }
                else
                {
                    if (c)
                    {
                        carry = 0;
                        *a |= b;
                    }
                }
            }

            b <<= 1;
        }
    }

  • spotcatbug (unregistered)

    er, WTF?

    Maybe this:

    void addEleven(unsigned int *a)
    {
        unsigned int    b = 1, c = 0;

        while (b != 0)
        {
            if ((b & 1) || (b & 2) || (b & 8))
            {
                if (*a & b)
                {
                    if (!c)
                    {
                        *a &= ~b;
                        c = 1;
                    }
                }
                else
                {
                    if (!c)
                        *a |= b;
                }
            }
            else
            {
                if (*a & b)
                {
                    if (c)
                        *a &= ~b;
                }
                else
                {
                    if (c)
                    {
                        carry = 0;
                        *a |= b;
                    }
                }
            }

            b <<= 1;
        }
    }

  • facade (unregistered) in reply to antareus

    Has no one on this thread heard of defensive programming?!?

    <FONT size=2>

    </FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2> (11 + i <= </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>.MaxValue) {i+=11;}

    </FONT> 

  • None (unregistered) in reply to dhromed
    dhromed:

    Brainfuck.
    <3


    I know, but must you show your (__!__) for that?
  • Anonymous (unregistered) in reply to Bob
    Anonymous:
    How can you read that?!?!  It has to be all on one LINE!!!

    ++i;++i;++i;++i;++i;++i;++i;++i;++i;++i;++i;



    You could improve readability even further by getting rid of most of those ++i's:

    i = 1 + (-3 * i) + ++i + ++i + ++i + ++i;
  • (cs)

    Well, of course, if this was Delphi, it'd be faster to decrement down to x+11... "But I don't have time for this (tm)"

  • Anonymous (unregistered) in reply to Anonymous
    Anonymous:

    i = 1 + (-3 * i) + ++i + ++i + ++i + ++i;


    Sorry, that was sub-optimized. It should be:

    i = -3 * i++ + ++i + ++i + ++i + ++i;
  • (cs) in reply to Effendi
    Anonymous:
    So, is lamecoder the only one here besides me who got the Spinal Tap reference?


    Nah, I got it too :-D

    class HardcoreInt
    {
    private:
        int m_int;
    public:
        void HardCoreInt(int nVolumeBelowTen)
        {
            m_int = VolumeBelowTen;
        }
        void CrankItToEleven()
        {
            int i = m_int;
            while(m_int lessthan i+11)
               m_int++;
        }
        void TakeItToTheLimit()
        {
            return m_int;
        }
    }
  • endothermal (unregistered) in reply to Bellinghman

    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.

     

    Looks like a user error to me :) No problem quoting here. Are all of the people complaining about the quoting system using Firefox?

     

  • spotcatbug (unregistered) in reply to endothermal
    Anonymous:

    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.

     

    Looks like a user error to me :) No problem quoting here. Are all of the people complaining about the quoting system using Firefox?

     


    I tried Firefox and IE. Neither worked well for me.
  • (cs) in reply to spotcatbug

    In Pascal/Delphi:

    i := i + 11; 

  • (cs) in reply to facade
    Anonymous:

    Has no one on this thread heard of defensive programming?!?

    <font size="2"> </font>

    <font color="#0000ff" size="2">if</font><font size="2"> (11 + i <= </font><font color="#0000ff" size="2">int</font><font size="2">.MaxValue) {i+=11;}</font>

     



    I really hope this was an intentional WTF... try

    <font color="#0000ff" size="2">if</font><font size="2"> (i <= </font><font color="#0000ff" size="2">int</font><font size="2">.MaxValue - 11) {i+=11;}</font>

  • Anonymous (unregistered) in reply to Pedant

    We are all missing a very important point here:  Why in God's name is he trying to "increment by 11"?  Can anyone think of a application of "var += 11" that is not a WTF in itself?

  • (cs) in reply to Anonymous

    Having 11 as a number out there is bad design. It needs to be encapsulated in its own class, otherwise HELLO MAINTENANCE HELL?!?!?!!?

    So do it like this:

    <FONT face="Courier New" size=2>class Step
    {
      private static int counter;
      public static boolean incCounter()
      {
        if (++counter >= 11)
          return false;
        return true;
      }
      public static int getCounter()
      {
        return counter;
      }
    }
    public class IncrementBy11
    {
      public static void main(String[] args)
      {
        int i =  (args.length > 0 ? Integer.parseInt(args[0]) : 8);
        while (Step.incCounter());
        System.out.println("Result = " + (i + Step.getCounter()));
      }
    }</FONT>

  • (cs)

    All those examples have a magic number in them - we should declare a constant to represent eleven first - in Javascript -

    //THIS IS A CONSTANT - DO NOT ASSIGN

    var g_const_eleven = 11;

     

  • AJR (unregistered)

    C preprocessor madness:


    #define inc11(x) do{int _counter_in_macro; for(_counter_in_macro=0;_counter_in_macro<11;_counter_in_macro++,x++);}while(0)

    ...

    inc11(i);

  • diaphanein (unregistered) in reply to AJR

    Because I can:

    template< int increment_by >
    int increment( int value )
    {
       return ( increment<I-1>(value) + 1 );
    }

    template<>
    int increment<0>( int value )
    { return value; }

    // usage:
    int i = ...;
    i = increment<11>( i );

  • diaphanein (unregistered) in reply to diaphanein
    Anonymous:

    Because I can:

    template< int increment_by >
    int increment( int value )
    {
       return ( increment<I-1>(value) + 1 );
    }

    template<>
    int increment<0>( int value )
    { return value; }

    // usage:
    int i = ...;
    i = increment<11>( i );

    In my hurry, I screwed up the implementation of the first func:

       return ( increment<I-1>(value) + 1 );

    should be:

       return ( increment<increment_by-1><I-1>(value) + 1 );

     

  • Travis Owens (unregistered) in reply to macman

    This would be cute except the problem with all the loops posted here is assuming that I is 1 or 0, and the guy is talking about incrimenting 11, not reaching 11.

    So I think you all meant to say something more like:

    <FONT face="Courier New">j=i + 11;
    while (i < j)
    {
       i++;
    }</FONT>

  • Stephan Rose (unregistered) in reply to Jasmine Strong

    Anonymous:
    Oldschool ARM hackers in the house.

    incbyeleven:
        STMFA   r13!, {r0-r1, r14 } ; save some work registers, return address
        ADR r0, token_i         ; get the address of static int i
        LDR r1, [r0]               ; load it into r1
        ADD r1, r1, #11         ; r1 += 11;
        STR r1, [r0]                ; back it goes
        LDMFA   r13!, { r0-r1, r15 }  ; return

    Man, you bring back memories of my time when I wrote an OS for the Cirrus Logic EP9315, which has an ARM core =)

  • (cs) in reply to Stephan Rose

    you guys are all very clever and your solutions are just wonderful.
    But i must say that none of you is up to news. The most recent version of Microsoft's C# compiler implements a new set of operators to make the life of poor programmers like Nathan much easier. I might even say the new operators are absolutely revolutionary.
    All you have to do now is use the new +eleven operator, in either prefix or postfix form.
    Or, if you're a real geek and you know what you want, you might want to use the new +? operator, which increments by exactly the number you have on your mind at the time of writing. Note, however, that the usage of +? operator greatly reduces code readability, so handle with care.

  • Evan M. (unregistered)

    What all of you have to take a look at is the context of this question. Most obviously, if he doesn't know how to increment a number, besides using the increment operator, then he must not know that you can assign to a variable a variable that's being accessed (i.e. i = i +1). Thus, we'll help him out:

    int i = x //initial value
    int b;

    b = i + 11;
    i = b;

    He's not a moron, just ignorant of some of the most basic syntactic programming protocols.

  • Daniel (unregistered)

    While using increments may challenge some programmers, why does using the word "increment" challenge all programmers?  Increment is a noun, not a verb.  Code can increase a value by an increment.  Until the dictionary folks change their minds, your code can't "increment a value."  "It uses an increment of 11" makes sense.  "To increment by 11" isn't English.

  • (cs) in reply to Daniel

    Daniel: "Until the dictionary folks change their minds, your code can't "increment a value.""

    Ah, Daniel, Daniel. You have misunderstood something fundamental about language. It evolves.

    Now, you may be under the weird impression that our language evolves by dictionary makers making decisions, and that everyone else follows along. Sadly, you are mistaken. It's the other way around:- the dictionary makers (and grammarians) document the common usages.

    Or, to put it another way - dictionaries are descriptive, not prescriptive.

    In our field, we increment. This is common usage and to deny it is foolish and wrong, however tempting.

  • (cs) in reply to Daniel

    I like the computer science approach:

    void increment( int * value, int inc ) {
        if (inc > 1) increment( value, --inc );
        (*value)++;
    }

    main() {
        int myVar;
        increment( &myVar, 11 );
    }


    Nothing like a little recursion to make my old college professors happy.

    But us real engineers know look up tables are the fastest:

    int inceleven[] = {11,12,13,14,15,16 }// ... etc

    But the best solution changes the base so a single ++ will increment by 11:

        int rem = i % 11;
        i /= 11;
        i ++;
        i *= 11;
        i += rem;


  • shrimpx (unregistered)

    To do it right you have to compute the number 11 a few times, some at compile time, some at runtime. Thanks C++ and its builtin staged computation abilities.

    #include <iostream>
    
    template <int n>
    struct num { enum { r = 1 + num<n-1>::r }; };
    
    template <>
    struct num<0> { enum { r = 0 }; };
    
    void incBy11(int &i) {
      const int p = i;
      do { i++; } while (i < p + num<0x1b&0xf>::r);
    }
    
    int main() {
      int i = 5;
      incBy11(i);
      std::cout << i << std::endl;
    }
    
  • (cs) in reply to Daniel
    Anonymous:
    While using increments may challenge some programmers, why does using the word "increment" challenge all programmers?  Increment is a noun, not a verb.  Code can increase a value by an increment.  Until the dictionary folks change their minds, your code can't "increment a value."  "It uses an increment of 11" makes sense.  "To increment by 11" isn't English.


    Since everyone here understands what the word "increment" means in this context, we'll continue using it.

    Therefore, it's a legitimate (albeit specialized within the computing industry) usage. Therefore, it's English - because we say it is. Anything in common usage that is understood should be regarded as English. Just because the dictionary folks haven't caught up doesn't mean that we have to stop using the word.

    If we let the language police dictate what's acceptable and what isn't, English will go the way of Latin and become an ancient, outdated language. I'm all for rules of grammar and spelling, but I'm also for linguistic evolution.

    We all understand the English verb "increment". You understood it too, but chose to deny its "authenticity" as a real English word, despite the fact that the intended meaning was perfectly conveyed, and the word did its job.

    It doesn't challenge me at all.
  • Jasmine Strong (unregistered) in reply to Stephan Rose
    Man, you bring back memories of my time when I wrote an OS for the Cirrus Logic EP9315, which has an ARM core =)

    I am not men, I am hot chicks.  :-)

    ARM cores are everywhere.  So much so that optimization can save lives.  Can you see where I could have saved an ARM11 cycle by using an extra register?
  • Oliver Klozoff (unregistered)

    Sadly, I expect that this won't be read by very many, since there are so many posts, but after seeing what the other guys had come up with, I had to throw in my own.

    Also, could someone please explain to me why I need to answer the correct captcha to preview the message?

    using System; using System.Text.RegularExpressions; using System.Reflection;

    namespace HowToIncrement { public abstract class Incrementer { // I tried to make this more generic, but it turns out that .NET's // innards for integers is somewhat less OO than you might think.

    	public int Value;
    
    	public Incrementer(int WhatValue) { Value = WhatValue; }
    
    	public override string ToString() { return this.Value.ToString(); }
    
    	protected int GetIncrementAmount()
    	{
    		string s = this.GetType().Name;
    		Regex re = new Regex(@"([\d.E+-]+)");
    		Match m = re.Match(s);
    		if (m == null) 
    		{
    			throw new Exception("Can't find increment amount in type name!");
    		}
    		return int.Parse(m.Value);
    	}
    
    	protected static Incrementer Do_Addition(Incrementer a, int addend, bool subtract) 
    	{
    		if (subtract) 
    		{
    			a.Value -= addend;
    		} 
    		else 
    		{
    			a.Value += addend;
    		}
    		return a;			
    	}
    
    	public static Incrementer operator-- (Incrementer a) 
    	{
    		return Do_Addition(a, a.GetIncrementAmount(), true);
    	}
    
    	public static Incrementer operator++ (Incrementer a) 
    	{
    		return Do_Addition(a, a.GetIncrementAmount(), false);
    	}
    
    }
    
    public class IncrementBy11 : Incrementer 
    {
    	public IncrementBy11(int val) : base(val) {}
    };
    
    public class IncrementBy12 : Incrementer 
    {
    	public IncrementBy12(int val) : base(val) {}
    };
    
    class Class1
    {
    	[STAThread]
    	static void Main(string[] args)
    	{
    		Incrementer x = new IncrementBy11(5);
    		Incrementer y = new IncrementBy12(24);
    		Console.WriteLine("x's initial value: {0}\ny's initial value: {1}", x, y);
    		x++;
    		y--;
    		Console.WriteLine("x after ++: {0}\ny after --: {1}", x, y);
    	}
    }
    

    }

  • Chris Brien (unregistered) in reply to tmeekins
    tmeekins:
    But the best solution changes the base so a single ++ will increment by 11:

        int rem = i % 11;
        i /= 11;
        i ++;
        i *= 11;
        i += rem;


    That is just beautiful. It means he can continue using the ++ operator to do exactly what he wants. I think, sir, you just Won.

  • Chris Brien (unregistered) in reply to Chris Brien

    Yep, this forum software is wtfworthy.

  • poser (unregistered)

    Some java code above was using too many constants, should be:

    ... incrementByEleven ...

    int eleven = 11

    return x + eleven

  • cm (unregistered)

    The loops are all too slow! You need a lookup table, initialized like so:

    int[] inc = new int[12];

    int f = 6;
    for (i = 1; i <= 11; i++) {
        inc[i] = f;
        f++;
    }

    Then later in the program you can do:

    i = i + inc[11] - 6;

    No, really.

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

Log In or post as a guest

Replying to comment #:

« Return to Article