Consider a sequence $a\_i$ defined by
$$
\begin{align\*}
a\_1&=p,\\
a\_2&=q,\\
a\_i&=a\_{i-1} \oplus a\_{i-2}+1,
\end{align\*}$$
where $\oplus$ is the bitwise xor operation. How can we give an upper bound for $a\_n$ as a function of $p,q,n$? 


There are lots of $p,q$ which satisfy $\mathop {\lim }\limits\_{n \to \infty } {a\_n} = \infty $, but it seems from experimentation that $a\_n=O(n)$. I do not have any idea how to prove it, nor do I know whether it is correct.  

If this problem is difficult to solve, I only need to prove that, for each $1 \le i \le n$, we have $a\_i \le 2^{12}$ when $p,q,n \le 1000$.



A Bitwise Xor Problem
$\def\U#1{\underline{#1}}\def\O#1{\overline{#1}}$The bound $a\_n=O(n)$ is true, in fact, we have $a\_n\le\max\{8p,8q,\frac{16}3n\}$. The argument is quite elementary, but a bit tedious to write down properly, hence I will only sketch it, and rely on the reader to fill in the details.


Rather than estimating $a\_n$ directly, we will try to bound the position of the most significant bit of $\max\_{m\le n}a\_m$. Notice that $\oplus$ cannot raise this quantity, and ${}+1$ can raise it only by one bit, when it results in a power of $2$ larger than all the previously seen numbers. In this case the ${}+1$ operation involves carries all the way from the $0$th bit to the new most significant bit. Thus, the crucial thing is to investigate carries in the sequence.


For any $k\ge1$, the operations of $\oplus$ and ${}+1$ are well defined modulo $2^k$, hence we can look at the sequence $a\_n\bmod2^k$. Since the sequence is uniquely reversible mod $2^k$ by $a\_{n-1}=a\_n\oplus(a\_{n+1}-1)$, it must be *periodic* mod $2^k$.


When computing $a\_n$, there is a carry from $(k-1)$th bit to $k$th bit iff $a\_n\equiv0\pmod{2^k}$; for $k>1$, this holds iff there was a carry to the $(k-1)$th bit, and the $(k-1)$th bit of $a\_n$ is $0$. Let us denote the $k$th bit of $a\_n$ as $a\_{n,k}\in\{0,1\}$.


**Case 1:** $a\_n\bmod2$ (that is, $a\_{n,0}$) is $\dots1111\dots$. Then there are no carries whatsoever, hence $a\_n=O(1)$ and we are done.


**Case 2:** $a\_n\bmod2$ is $\dots001001001\dots$ of period $3$. Carries from $0$th to $1$st bit happen at the positions of the $0$s in the period. (Here and below, the pattern shown is not necessarily aligned with the start of the sequence, hence strictly speaking the period is one of its cyclic shifts.)


This again splits to two subcases:


**Case 2a:** $a\_n\bmod4$ is $\dots122320122320\dots$ of period $6$. That is, the sequence of $a\_{n,1}$ is $\dots\O{\U0}\U11\U1\U10\dots$, where underlines signify carries from the $0$th bit to the $1$st bit, and overlines carries from the $1$st bit to the $2$nd bit.


**Case 2b:** $a\_n\bmod4$ is $\dots120300120300\dots$ of period $6$; that is, the sequence of $a\_{n,1}$ is $\dots0\U1\O{\U0}1\O{\U0}\O{\U0}\dots$.


I claim that in Case 2a, for all $k\ge2$, the sequence $a\_n$ has period $3\cdot2^{k-1}$ modulo $2^k$, and there is carry to the $k$th bit at exactly one position per period.


This is proved by induction on $k$. It holds for $k=2$. When going from $k$ to $k+1$, we see that the sequence $a\_{n,k}$ is a $\oplus$-linear combination of:


1. A solution of the homogeneous recurrence $b\_n=b\_{n-2}\oplus b\_{n-1}$ modulo $2$. This is either $\dots00000\dots$, or (some shift of) $\dots011011011\dots$.
2. One particular solution of $b\_n=b\_{n-2}\oplus b\_{n-1}\oplus c\_n$, where $c\_n$ is the sequence of carries to the $k$th bit. By the induction hypothesis, $c\_n$ is $3\cdot2^{k-1}$-periodic, and the period has the form $100\dots0$. Thus, one solution $b\_n$ is given by the $3\cdot2^k$-periodic sequence consisting of $110110110\dots110$ for $3\cdot2^{k-1}$ positions, followed by $00\dots0$ for $3\cdot2^{k-1}$ positions.


Clearly, this makes $a\_{n,k}$ (and thus $a\_n\bmod2^{k+1}$) $3\cdot2^k$-periodic. Carries to the $(k+1)$th bit may happen only at the two positions in this period that correspond to carries to the $k$th bit; these two positions are at distance $3\cdot2^{k-1}$ apart, and they are occupied by two opposite bits: the sequence from 1. gives them the same value, whereas the sequence from 2. gives them opposite values. Thus, exactly one of them gives rise to a carry to the $(k+1)$th bit.


In Case 2b, a similar inductive argument establishes the following claim: for any $k\ge2$, $a\_n\bmod2^k$ is $3\cdot2^{k-1}$-periodic, and there are exactly $3$ carries to the $k$th bit in each period. Two of these carries have the same position modulo $3$, whereas the third is at relative position $2$ modulo $3$ with respect to them.


This implies a bound on $a\_n$ as follows. For a given $n$, let $k$ be the least integer such that $n<3\cdot 2^k$ and $p,q<2^{k+1}$. Thus, $2^k\le\max\{p,q,2n/3\}$. We know from above that up to $a\_n$, there are at most three carries to the $(k+1)$th bit. The first of these will attain the value $2^{k+1}$, the other two may at most get to $2^{k+3}$. Thus, the most significant bit of $a\_n$ is at position at most $k+3$. Actually, taking into account the relative position of the carries modulo 3, one can check that the most significant bit can only become at most $k+2$. Thus, $a\_n<2^{k+3}\le\max\{16n/3,8p,8q\}$.


