In this case the template meta-programming solution is rather more concise :-

template<int N>
struct B {
    enum {Ndiv10 = N / 10};
    enum {Nrem10 = N - 10*Ndiv10};
    enum {R = Nrem10 + 2*B<Ndiv10>::R};
};

template<>
struct B<0> {
    enum {R = 0};
};

...

const int mask = B<100110>::R;