I want to optimize a function that determines whether a given number $n$ is EITHER (a power of 2) OR (the sum of powers of 2). Using, this answer, it appears that a sum of power of 2s contain at most two 1s in binary representation. If my observation is incorrect, it defeats the purpose of this question so please state the same otherwise read on.
Let $f(n)$ be $true$ if the binary representation of $n$ contains at most two 1s.
I am working with a 2D grid of such numbers and currently calculate the following as unrelated operations:
- $a=f(x)$
- $b=f(y)$
- $c=f(x+y)$
- $d=f(x-y)$
Is it possible to reduce these four distinct computations by inferring one from the other?