TITLE: Greatest possible cardinality of a set of numbers less than 1000 such that the sums avoid powers of 2
QUESTION [8 upvotes]: Let $S = \{1, 2, \dotsc, 1000\}$, $T$ is a subset of $S$. For any $a, b\in T$ (can be the same), $a + b$ is not a power of $2$. Find the greatest possible value of $|T|$.

Notice that if $a+a=2^N=2a$, then $a$ must be a power of $2$ as well. Namely $2^{N-1}$. Hence we can't include any power of 2 in the set of $T$ as $a$ and $b$ could be the same.
That leaves us for $T=\{1,3,5,6,...,513,514,...,1000\}$. Now notice that if $1+b=2^N$, then $b=2^N-1$, which means every number such that it is one less than a power of $2$ can't be included inside, or $1$ could not be inside.
Similar for $3, 5, 6, \dotsc$ as a number with $1, 3, 5, \dotsc$ less than a power of $2$ is much more than a single number $1, 3, 5$ etc. Hence I think it would be more clever to exclude the single number $1, 3, 5, \dotsc$ (I don't know if it's right).
From here on I'm stuck and I have no idea.

REPLY [5 votes]: According to the approach described by Henning Makholm if $S=\{1,2,\dots,n\}$ it seems that the maximum size  of such set $T(n)$ is related to $[n]_2$ (the binary expansion of $n$). It should be
$$|T(n)|=\frac{n-\mbox{number of runs in $[n]_2$}}{2}$$
For $n=1000$ then $[n]_2=1111101000$ and
$$|T(1000)|=\frac{1000-4}{2}=498.$$
Is there any connection  between Gray code and this problem?