J
Joona I Palaste
Reading Skybuck Flying's obvious troll message, I thought of how to
properly do a for loop that would iterate over the complete range of
an unsigned variable. As you all know, Skybuck's method won't work.
There are a number of other ways. You could use a wider type to do the
counting, but that would be cheating. You could also handle either the
first or last iteration as a special case outside the loop, but then it
would be more than a loop. You could make a very big array of integer
flags to tell whether you've already visited a value, but that would
consume too much memory.
So I settled down to this version. Assume unsigned short is 16 bits
wide.
int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}
Any more elegant solutions out there?
properly do a for loop that would iterate over the complete range of
an unsigned variable. As you all know, Skybuck's method won't work.
There are a number of other ways. You could use a wider type to do the
counting, but that would be cheating. You could also handle either the
first or last iteration as a special case outside the loop, but then it
would be more than a loop. You could make a very big array of integer
flags to tell whether you've already visited a value, but that would
consume too much memory.
So I settled down to this version. Assume unsigned short is 16 bits
wide.
int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}
Any more elegant solutions out there?