[OT] Money for nothing, and your bit flips for free?

plug.org at 2nerds.com plug.org at 2nerds.com
Wed Feb 7 19:27:36 MST 2007


On Wed, 7 Feb 2007, Steve wrote:
> Which got me thinking that maybe a standard while loop is not the
> proper solution here.  So I turned it on it's head with a do while
> which gave me 100,000 which is a pretty good speed up!
> 
> 	do{
> 		s[y] = ~s[y];
> 	}while(y++ <= s.length());

Unless I misunderstand the meaning of the value returned by s.length(), 
that do/while loop twiddles two characters past the end of the data in the 
array.

My presumption is that if s.length() returns 3, for example, the valid 
array indices would be 0, 1, and 2.  If s.length() returns 3, however, the 
loop above would twiddle the array entries at indices 0, 1, 2, 3, and 4.  
Putting the test at the end of the loop results in a double-whammy from 
the use of both post-increment and '<='.

If you're fond of do/while, try this instead:
	do {} while(++y < s.length());

Chris



More information about the PLUG mailing list