0

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.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
Kevin
  • 1,151
  • 1
  • 10
  • 18

1 Answers1

2

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.

simonzack
  • 19,729
  • 13
  • 73
  • 118
  • The count of each possible base 10 digit in x, i.e., each 6-bit wide field in the output should hold the count of the digits corresponding to that offset in x. Bits 0-5 holds the number of 0's, bits 6-11 the number of 1's, 12-17 the number of 2's, etc. – tvanfosson Nov 29 '14 at 03:52