CazadorDivino's blog

By CazadorDivino, 9 years ago, In English

I was solving TCO 2015 and saw this code, someone knows it for?

public static int getMask(int n){
    	return n != 0 ? 1 << n % 10 | getMask(n / 10) : 0;
    }
  • Vote: I like it
  • +17
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it +11 Vote: I do not like it

the method build a set where the k-th bit is 1 if the the digit k appears in N, 0 <= k <= 9.

Example, N = 42594 getMask(N) = 1000110100 in binary, notice that the rightmost bit represents the status of the digit 0.