Bitwise-OR Operations on Random Integers
Let y_0, y_1, y_2,... be a sequence of random unsigned 32-bit integers (each bit independently 0 or 1 with equal probability). Define x_0 = 0 and x_i = x_(i-1) mathbin| y_i for i >= 1. Let N be the...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $y_0, y_1, y_2, \dots$ be a sequence of random unsigned $32$-bit integers
(i.e. $0 \le y_i < 2^{32}$, every value equally likely).
For the sequence $x_i$ the following recursion is given:
$x_0 = 0$ and
$x_i = x_{i - 1} \boldsymbol \mid y_{i - 1}$, for $i > 0$. ($\boldsymbol \mid$ is the bitwise-OR operator).
It can be seen that eventually there will be an index $N$ such that $x_i = 2^{32} - 1$ (a bit-pattern of all ones) for all $i \ge N$.
Find the expected value of $N$.
Give your answer rounded to $10$ digits after the decimal point.
Problem 323: Bitwise-OR Operations on Random Integers
Mathematical Foundation
Lemma 1 (Bit independence). For each bit position , let be the first index such that bit of equals 1. Then (supported on ), and are mutually independent.
Proof. The event “bit of equals 1” has probability and is independent across both and . Hence is the waiting time for the first success in independent Bernoulli trials, giving . Independence of the ‘s follows from the bitwise independence of the ‘s.
Theorem 1 (CDF of the maximum). and
Proof. has all bits set iff every . By independence, .
Theorem 2 (Closed-form expectation).
Proof. Using the tail-sum formula for non-negative integer-valued random variables:
Expand via the binomial theorem:
Therefore
Summing over and interchanging summation (justified by absolute convergence, since for ):
Lemma 2 (Convergence of the tail sum). The series converges.
Proof. For large , by Bernoulli’s inequality. Since , the series converges by comparison.
Editorial
E[N] = sum_{j=1}^{32} (-1)^{j+1} * C(32,j) / (1 - 2^{-j}) where N is the first time all 32 bits are set after OR-ing with random 32-bit integers.
Pseudocode
s = 0
For j from 1 to 32:
s += (-1)^(j+1) * C(32, j) / (1 - 2^(-j))
Return s // evaluate with sufficient floating-point precision
Complexity Analysis
- Time: where is the number of bits. The computation is a single loop of 32 terms.
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// E[N] = sum_{k=0}^{inf} [1 - (1 - 2^{-k})^32]
// Converges very fast. Compute directly by summing enough terms.
long double result = 0.0L;
for (int k = 0; k <= 200; k++) {
long double p = 1.0L - powl(2.0L, -(long double)k);
long double p32 = powl(p, 32.0L);
result += 1.0L - p32;
}
cout << fixed << setprecision(10) << result << endl;
return 0;
}
"""
Problem 323: Bitwise-OR Operations on Random Integers
E[N] = sum_{j=1}^{32} (-1)^{j+1} * C(32,j) / (1 - 2^{-j})
where N is the first time all 32 bits are set after OR-ing with random 32-bit integers.
"""
from math import comb
from decimal import Decimal, getcontext
def solve():
getcontext().prec = 50
result = Decimal(0)
for j in range(1, 33):
sign = Decimal(1) if j % 2 == 1 else Decimal(-1)
c = Decimal(comb(32, j))
denom = 1 - Decimal(2) ** (-j)
result += sign * c / denom
# Round to 10 decimal places
print(f"{result:.10f}")
if __name__ == "__main__":
solve()