- 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
And for the paranoids who don't trust their processors addition instruction:
Admin
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.
Admin
#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;
}
Admin
Admin
Ahem... ours are on purpose [:D] At least mine was.
Admin
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.
Admin
int j = i + 11;
while(i++ < j) {}
Admin
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>
Admin
What about Duff's Device?
Admin
seriously this board software is the real WTF
while(i++ lessthan j) {}
really not sure how to post a code block in this =)
Admin
And for the paranoid who don't trust their processors addition instruction:
(Space added to bitshifts to placate the forum software.)
Admin
let's get that right...
It'll still break for when 11 <= 0, but you can't win them all.Admin
I think you ALL have missed the answer. It is obviously:
i += 3; // Increment i by 11.
Admin
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?"
Admin
The < version will eventually terminate when i overflows. !=
Admin
You are all missing the simplest solution.
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.
Admin
And now we need to put it in a function and name it aptly:
Feel free to use this code in C, C++, Java or C# - good code is PORTABLE!
Here's my own version:
I'm not responsible for any loss of precision you might encounter. Get better integers.
Admin
No one has done it via bit-shifting and modulo operators, which is what I would do if I had the time/masochism.
Admin
typedef int j;
typedef struct i { j i; } i;
j incrementByEleven(j I) { j j; i i = { .i = I };
}
j main(void) { incrementByEleven(0); incrementByEleven(3); incrementByEleven(4); }
Admin
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!
Admin
Wow, the code-tag on this board is a sexy little wtf in its own right.
Admin
This should work for unsigned numbers (spacing intended ;)):
while (j>=i + 11<=!j ) ++j;
i^=j^=i^=j;
Admin
Tricky code box o_O
while (j>=i + 11< =!j ) ++j;
i^=j^=i^=j;
Admin
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 ++ !
Admin
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 ?
Admin
Admin
Forgot to mention: the previous example is your typical PHP/MySQL job.
Admin
I can't believe that not a single person has posted an answer that requires the C preprocessor to do something magi-tragic.
Admin
10++ ?
Admin
#define PLUS +
#define INCREMENTBY +=
#define ONE 1
#define TEN 10
#define ELEVEN TEN PLUS ONE
int X = 0;
X INCREMENTBY ELEVEN;
Admin
In BLOCKED SCRIPT
Admin
Not such a WTF really, in Delphi / Pascal it would be...
inc(i,11);
Admin
Personally I prefer
i -= -11
or
#define IPLUSELEVEN (i+11)
i = IPLUSELEVEN
Drak
Admin
// +>+++++[-<++>]
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]++;
}
Admin
<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>
Admin
oops....
list( $i ) = $row = ...
should be
list( $i ) = ...
Admin
int i = 0;
(((((((((((i++)++)++)++)++)++)++)++)++)++)++);
off course :-)
Admin
Good thing that you showed us all how to do it.. all of us here would have used the for loop...
Admin
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());
Admin
Guys, you forgot exception handeling, what if an exception is raised when you increment by 11 [:P]
Admin
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>
Admin
Replace i=(i<<z) above with i=(i<<z)|k;
It got stripped out of the post for some reason...
Admin
You need to use Ook!.net it is much more suitable for this problem
Ook?
Ook.
Admin
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"... :-)
Admin
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!
Admin
May I suggest an optimisation: ans = addNumber(++i, --n);
Admin
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.
Admin
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
Admin
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]
I'm sure he does some of his best thinking when he's asleep. [6]
Admin
int i = 1;
while (true != false)
i = i + 11;