# Counting one’s in a stream of bits

I have to count the number of one’s in last $$m$$ bits in a stream of bits and $$m \leq n,$$ where $$n$$ is the window size and it should take polylogarithmic space in $$n$$. I could only store last $$n$$ bits and then count the number of one’s in those $$n$$ bits but it takes $$O(n)$$ space. How to achieve it in $$O(\log^k n)$$, $$k>0$$.
Any hint would be appreciated.