in the other thread he was talking about a "string", but i think the arithmetics (or other possible ways hot to solve the problem) wont differ much f rom a case where an array of 0 and 1 is the desried inout/output.
Yeah I don't know what he's talking about really, but yes you're right the algorithm will look fairly similar either way.
But then again I ponder if it's what I suspect now... it's part of the stdlib- atoi / itoa(or sprintf on compilers that lacks itoa) and none of the stdlib implementations are "bad".
However I ponder if there is a need to optimise this further- it feels like the optimisation efforts are spent in the wrong area, a good algorithm doesn't convert between types at a time critical point as that's always something that costs and doesn't really give any benefit- the data is the same but stored in a different format.
---
Additionally yes the first example makes sense... somewhat if it's equivalent to atoi.
Just another thing- comparisions and branches is something you really don't want in time critical code, especially not on a superscalar CPU as a branch prediction miss or pipeline stall costs way way way way more than an unnecessary calculation, in fact unnecessary calculations are often free.
Also some other fundamental rules- especially applying to PowerPC.
An addition or subtraction is about 1 cycle.
A multiplication is around 5 - 8 cycles.
A division is around 35 cycles.
If you can use shifts instead of divisions or multiplications that will always be faster... a shift is 1 cycle (the PowerPC has a barrel shifter), it also has extremely useful shift and mask instructions.
For fixed point or integer values shift as much as possible, there's not a single architecture that is faster to calculate than shift.
If you really have to use (if using floating point forexample) multiplication or division it's faster (and if you use Altivec you have to- as it don't support division) to precompute (with the preprocessor in the best case) the divisor as a percentage and use multiplication instead.
Ie. res = number/35 is equivalent to #define DIVIDE_BY_THIRTYFIVE (1.0/35.0) res = number*DIVIDE_BY_THIRTYFIVE;