Count Total Set Bits Time : O(logA) Space : O(1) 123456789int Solution::solve(int A) { if(A <= 1) return A; long long lg2 = log2(A), mask = 1<<lg2, mod = 1e9 + 7; long long res = 0, shift = 0; while((1<<shift) < mask) res = (res * 2 + (1<<shift++)) % mod; return (res + ((A^mask) + 1) + solve(A ^ mask)) % mod;}