<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://mxksm.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://mxksm.github.io/" rel="alternate" type="text/html" /><updated>2026-04-21T00:34:34+00:00</updated><id>https://mxksm.github.io/feed.xml</id><title type="html">Maksim Lavrenko</title><subtitle>personal description</subtitle><author><name>Maksim Lavrenko</name><email>maksim4lavrenko@gmail.com</email></author><entry><title type="html">XOR of Consecutive Integers</title><link href="https://mxksm.github.io/posts/2024/11/bitwise/" rel="alternate" type="text/html" title="XOR of Consecutive Integers" /><published>2024-11-09T00:00:00+00:00</published><updated>2024-11-09T00:00:00+00:00</updated><id>https://mxksm.github.io/posts/2024/11/bitwise</id><content type="html" xml:base="https://mxksm.github.io/posts/2024/11/bitwise/"><![CDATA[<h1 id="preliminaries">Preliminaries</h1>

<p>Let $A \oplus B$ be the bitwise XOR of $A$ and $B$.
For each pair of bits, the table below shows the result of XOR.</p>

\[\begin{array}{|c|c|c|}
\hline
A &amp; B &amp; A \oplus B \\
\hline
0 &amp; 0 &amp; 0 \\
0 &amp; 1 &amp; 1 \\
1 &amp; 0 &amp; 1 \\
1 &amp; 1 &amp; 0 \\
\hline
\end{array}\]

<p>This means that for any $A$, $A \oplus A = 0$ and $A \oplus 0 = A$.
In the context of bitwise operations, a number is the inverse of itself and 0 is the identity element.</p>

<h1 id="properties">Properties</h1>

<p><strong>Theorem 1</strong> Let $A$ be an even natural.
Then, $A \oplus (A+1) = 1$.</p>

<p><strong>Proof</strong>
Let $A$ be a natural
Then, $2\cdot A$ is $A$ with a 0 added to the right.
Similarly, $2\cdot A + 1$ is $A$ with a 1 added to the right.
Taking the XOR of $2\cdot A$ and $2\cdot A + 1$ gives us</p>

\[\begin{aligned}
2\cdot A \oplus (2\cdot A + 1) &amp;= A0_2 \oplus A1_2 = 0\cdots 0 1 = 1.
\end{aligned}\]

<p>Thus, we have shown that the XOR of an even number and the next odd number is 1.</p>

<p>From here, we can extend this property to the XOR of 4 consecutive numbers.</p>

<p><strong>Theorem 2</strong> Let $A$ be an even natural.
Then,</p>

\[\begin{aligned}
A \oplus (A+1) \oplus (A+2) \oplus (A+3) = 0
\end{aligned}\]

<p><strong>Proof</strong>
By the previous theorem,</p>

\[\begin{aligned}
    (A+0) \oplus (A+1) &amp;= 1 \\
    (A+2) \oplus (A+3) &amp;= 1
\end{aligned}\]

<p>Then, $A \oplus (A+1) \oplus (A+2) \oplus (A+3) = 1 \oplus 1 = 0$.</p>

<p><strong>Theorem 3</strong> Let $A$ be an odd natural.
Then, $A \oplus (A+1) = 2^n - 1$ where $n$ is some positive integer as described below.</p>

<p><strong>Proof</strong> Since $A$ is odd, it must end in a 1 in binary.
Then, $A+1$ will be $A$ with the rightmost 0 flipped to a 1 and all the bits to the right of it flipped to 0.
Let $n$ be the position of such a bit.
The XOR of $A$ and $A+1$ of the bits before the $n$th bit will be 0 because they are the same.
The XOR of the $n$th bit will be 1 because they are different.
The XOR of the bits after the $n$th bit will be 1 because by definition of $n$, the bits in $A$ must all be 1 and the bits in $A+1$ must all be 0.
Thus, the XOR of $A$ and $A+1$ will be $2^n - 1$, or simply n 1s in binary.</p>

<p><strong>Theorem 4</strong> Let $A$ be an odd natural.
Then,</p>

\[\begin{aligned}
    A \oplus (A+1) \oplus (A+2) \oplus (A+3) = 2^n - 4
\end{aligned}\]

<p>where $n$ is some positive integer as described below.</p>

<p><strong>Proof</strong>
By the previous theorem,</p>

\[\begin{aligned}
(A+1) \oplus (A+2) \oplus (A+3) \oplus (A+4) &amp;= 0
\end{aligned}\]

<p>Let $X$ be the XOR of the 4 consecutive numbers.
Then,</p>

\[\begin{aligned}
X &amp;= A \oplus (A+1) \oplus (A+2) \oplus (A+3) \\
A \oplus (A+4) \oplus X &amp;= A \oplus A \oplus (A+1) \oplus (A+2) \oplus (A+3) \oplus (A+4) \\
A \oplus (A+4) \oplus X &amp;= 0 \\
X &amp;= A \oplus (A+4)
\end{aligned}\]

<p>The proof is similar to the proof of Theorem 3, with only difference being that $n$ is the position of the rightmost $0$ in $A$ ignoring the last 2 bits.</p>

<p><strong>Definition</strong>
Let $f(A): \mathbb{N}_0 \to \mathbb{N}_0$ be a function that returns the XOR of all numbers from $0$ to $A$ inclusive.
That is, $f(A) = 0 \oplus 1 \oplus 2 \oplus \cdots \oplus A$.</p>

<p><strong>Corollary</strong> Let $A$ and $B$ be numbers such that $A &lt; B$.
Then, $f(A-1) \oplus f(B) = A \oplus (A+1) \oplus \cdots \oplus B$.</p>

<h1 id="generalization">Generalization</h1>

<p><strong>Theorem 5</strong>
Let $A$ be a natural.
Then,</p>

\[\begin{aligned}
    f(A) &amp;=
    \begin{cases}
        A &amp; A \equiv 0 \pmod 4 \\
        1 &amp; A \equiv 1 \pmod 4 \\
        A+1 &amp; A \equiv 2 \pmod 4 \\
        0 &amp; A \equiv 3 \pmod 4 \\
    \end{cases}
\end{aligned}\]

<p><strong>Proof</strong>
The proof by cases is trivial with the theorems established above.</p>]]></content><author><name>Maksim Lavrenko</name><email>maksim4lavrenko@gmail.com</email></author><category term="bitwise operations" /><category term="XOR" /><summary type="html"><![CDATA[Preliminaries]]></summary></entry></feed>