- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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.
Admin
(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
Admin
Sorry, should be Y = 5.5 initially, duh.
Admin
Couldn't resist. I expect the formatting will be screwed when I post this though.
Admin
"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.
Admin
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
Admin
Nice "Spinal Tap" content. Well done!
Fun,
Sten
Admin
Why not do it as a recursion?
Actually I'am not sure if that static will work correctly in recursion.. At least it should.
Admin
int target = i + 14;
while(i != target)
{
i = rand();
}
//Solutions analogous to the above are not rare.
Admin
Brainfuck.
<3
Admin
Strings!
Admin
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***
Admin
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!
Admin
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();
}
int addEleven(int i) { return bogoAdd(i, 11); }
The implementation of the destroyUniverse() function is left as an exercise for the reader.
Admin
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!
Admin
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);
}
}
}
Admin
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.
Admin
In C:
Admin
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;
}
}
Admin
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>
Admin
I know, but must you show your (__!__) for that?
Admin
You could improve readability even further by getting rid of most of those ++i's:
i = 1 + (-3 * i) + ++i + ++i + ++i + ++i;
Admin
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)"
Admin
Sorry, that was sub-optimized. It should be:
i = -3 * i++ + ++i + ++i + ++i + ++i;
Admin
Nah, I got it too :-D
Admin
Looks like a user error to me :) No problem quoting here. Are all of the people complaining about the quoting system using Firefox?
Admin
I tried Firefox and IE. Neither worked well for me.
Admin
In Pascal/Delphi:
i := i + 11;
Admin
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>
Admin
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?
Admin
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>
Admin
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;
Admin
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);
Admin
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 );
Admin
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 );
Admin
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>
Admin
Man, you bring back memories of my time when I wrote an OS for the Cirrus Logic EP9315, which has an ARM core =)
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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;
Admin
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.
Admin
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.
Admin
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?
Admin
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.
}
Admin
That is just beautiful. It means he can continue using the ++ operator to do exactly what he wants. I think, sir, you just Won.
Admin
Yep, this forum software is wtfworthy.
Admin
Some java code above was using too many constants, should be:
... incrementByEleven ...
int eleven = 11
return x + eleven
Admin
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.