2. For any fixed positive integer $m$, define the infinite sequence $\left\{a_{n}\right\}(n \geqslant 1): a_{1}$ is a positive integer, for any $n \geqslant 1$,
$$
a_{n+1}=\left\{\begin{array}{ll}
a_{n}^{2}+2^{m}, & a_{n}<2^{m} \\
\frac{a_{n}}{2}, & a_{n} \geqslant 2^{m} .
\end{array}\right.
$$

For each $m$, determine all possible values of $a_{1}$ such that all terms in $\left\{a_{n}\right\}$ are integers.
2. Suppose the values of $m$ and $a_{1}$ are such that all terms in the sequence $\left\{a_{n}\right\}$ are integers.

For each $i \geqslant 1$, let $a_{i}=b_{i} \cdot 2^{c_{i}}$, where $b_{i}$ is an odd number and $c_{i}$ is a non-negative integer.
Lemma 1 For any $i$, there exists $b_{i}2^{m}$ and an integer that minimizes $c_{i}$.
From $a_{i} \geqslant b_{i}>2^{m}$, we know $a_{i+1}=\frac{a_{i}}{2}$.
Thus, $b_{i+1}=b_{i}$, and $c_{i+1}=c_{i}-1m$, then $a_{i+1}=2^{m}\left(b_{i}^{2} \cdot 2^{2 c_{i}-m}+1\right)$.
Hence $b_{i+1}=b_{i}^{2} \cdot 2^{2 c_{i}-m}+1>b_{i}$.
(2) If $2 c_{i}b_{i}$.
(3) If $2 c_{i}=m$, then $a_{i+1}=2^{m+1} \cdot \frac{b_{i}^{2}+1}{2}$.
Notice that, $b_{i}^{2}+1 \equiv 2(\bmod 4)$.
Thus, $\frac{b_{i}^{2}+1}{2}$ is an odd number, and therefore,
$b_{i+1}=\frac{b_{i}^{2}+1}{2} \geqslant b_{i}$.
Lemma 1 and 2 are proved.
By Lemma 1 and 2, there must exist some index $j$ such that for any $k \geqslant j$, we have $b_{k}=b_{j}$.

Since when $a_{n} \geqslant 2^{m}$, we always have $a_{n+1}=\frac{a_{n}}{2}$, thus, the sequence $\left\{a_{n}\right\}$ contains infinitely many terms less than $2^{m}$. Therefore, we can take some $i>j$ such that $a_{i}<2^{m}$.

From the proof of Lemma 2, we know that only when $2 c_{i}=m$ and $b_{i+1}=b_{i}=1$ can both $a_{i}<2^{m}$ and $b_{i+1}=b_{i}$ be satisfied simultaneously. Therefore, $b_{i}=1$.

Since $\left\{b_{n}\right\}$ is non-decreasing, then $b_{1}=b_{2}=\cdots=1$, i.e., $a_{1}, a_{2}, \cdots$ are all powers of 2.
From $a_{i}=2^{c_{i}}=2^{\frac{m}{2}}<2^{m}$, we get
$$
\begin{array}{l}
a_{i+1}=2^{m+1}, a_{i+2}=2^{m}, \\
a_{i+3}=2^{m-1}, a_{i+4}=2^{2 m-2}+2^{m} .
\end{array}
$$

We have already proved that all terms in $\left\{a_{n}\right\}$ are powers of 2, so $2^{2 m-2}+2^{m}$ must be a power of 2. Clearly, this is only true when $2 m-2=m$.
Therefore, $m$ can only be 2.
For $m=2$, if $a_{1}=2^{l}(l \geqslant 1)$, then the terms in the sequence $\left\{a_{n}\right\}$ are $2^{l}, 2^{l-1}, \cdots, 4,2,8,4,2,8, \cdots$, which meets the requirements.
If $a_{1}=1$, then $a_{2}=5, a_{3}=\frac{5}{2}$, which does not meet the requirements.
In summary, only when $m=2$ do the required $a_{1}$ exist, in which case, all possible values of $a_{1}$ are
$$
a_{1}=2^{l}\left(l \in \mathbf{Z}_{+}\right) .
$$