Trying to understand an inline function

69 views Asked by At

I am studying the following function:

inline xint dtally(xint x)
{
    xint t = 0;
    while (x) t += 1 << ((x % 10) * 6), x /= 10;
    return t;
}

I just want to know what makes this feature i.e. which computes and stored in the variable t.

1

There are 1 answers

1
simonzack On

This counts the number of base 10 digits in the number x in t, separated by 6-width bit fields.

Note that each shift length is a multiple of 6. So if a digit is 0 the shift is 0, if the digit is 1 the shift is 6, if the digit is 9 the shift is 54, and so forth.

The reason 6 is used I think is so it fits under 64 bits.