In total, the outer loop executes the inner loop i times for each i in series 2^0, 2^1, ..., 2^(ceil(lg(N)) - 1) (the first ceil(lg(N)) powers of 2).
The resulting sum is hard to write in this forum, but in natural language it's something like sum from p = 0 to (ceil(log2(N)) - 1) of (sum from i = 1 to 2^p of 1)
Which is in O(N).
Thanks for the replies. I think I've finally understood it now. I just need final confirmation for the second question, when I change "i = i*2" to "i++", it's O(n^2) right?