explain Jack Purdum great code

Hi all, I am reading the Beginning C for Arduino. Its a great book. The Author of the book talks about a piece of code that he saw briefly , but does not explain what its means. Can some please explain, what it means.

i=(i<<3) +(i<<1) +(*string - ‘o’);

Thank

Alas, I don’t remember it. In fact, it was probably used as a demonstration of a bad way of doing something. Anyway, suppose i = 1. Then i << 3 would resolve to 8. The expression i << 1 then shifts i one more position to the left. Since each shift is the same at multiplying by 2, i would now be 16. Next, we have a pointer that is probably pointing to a digit character. Let’s say it points to the digit character ‘5’. Since the ASCII representation for ‘5’ is 53 and a zero character is 48, 53-48 = 5. So the expression resolves to:

i = 00000001 << 3 + 000000001 << 1 + (‘5’ - ‘0’);

Since operator precedence works from left to right by associativity rules, we get:

i = 00000001 << 3 + 000000001 << 1 + (‘5’ - ‘0’);
i = 8 + 000000001 << 1 + 5;
But, since i is now 8:
i = 00001000 << 1 + 5;
i = 00010000 + 5;
i = 16 + 5;
i = 21;

I think it was at this point where I said the programmer should be fired because it’s difficult to read.

i = 8 + 000000001 << 1 + 5;
But, since i is now 8:

i is NOT 8 at that point. putting i<<3 on the right side of an expression does not change the value of i. (it’s NOT the same as “i<<=3”)

“i = i<<3 + i<<1;” would be a relatively normal implementation of “i = i *10;”
I don’t know why you’d add *string-‘o’

westfw:
I don't know why you'd add *string-'o'

I think jack has the string part right, I also think its '0' not 'o', allowing you to produce offsets from Ascii numbers. Would be quicker than doing an atoi operation on a single value.

Ah. if it's 0, you get the standard ascii-to-decimal conversion innards:
n = n*10 + nextDigit();
So if you had 314, you'd start with n=0
n = 0 * 10 + 3 = 3
n = 3 * 10 + 1 = 30 + 1 = 31
n = 31 * 10 + 4 = 310 + 4 = 314 (ta da!)

Yep...I messed it up, but my heart's in the right place. Much too late for someone as old as I am to try and answer a question.