TITLE: Difference of powers of 2
QUESTION [5 upvotes]: Find the number of positive integers from $1$ to $1000 $ (inclusive) that can be expressed as the difference of two numbers in the set $\{1,2,2^2,2^3,\ldots\}$. I know how to solve this but the counting is becoming difficult.

REPLY [9 votes]: A positive number is the difference of two powers of two if and only if its representation in binary contains no more than one consecutive string of digits $1$. For numbers less than $2^{10}=1024$ you've got $10$ bit positions available, and you want to set bits in positions$~i$ with $m\leq i<n$ where $0\leq m<n\leq10$; there are $\binom{11}2=55$ such pairs $(m,n)$. (Incidentally your number will be $2^n-2^m$, so using binary representation is not really necessary here.) Among those, the ones that are greater than $1000=1111101000_2$ are those with $n=10$ and $m<5$, five of them, leaving $50$ true solutions.