Assume N is a power of 2; e.g., N = 2k
for(int i = 1; i < N; i *= 2) { for(int j = 0; j < i; j++) { sum++; } }