Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Coding Interview Patterns  Module 17 – Bit manipulation

Bitwise AND of a range: powers of two, reversal

The bitwise AND of every integer in a range, the next power of two for a buffer and a word with its bits reversed: the problems and the brute force.

  • Intermediate
  • 20 minutes
  • Examples run with Python 3.14.8, Pyodide 314.0.7 and Java temurin-25.0.4+101.0.LTS
  • By MySmartCoPilot
Free intro, Pro: 5 of 20 minutes free
Free: Three problems about where the bits are · The brute force, and why it fails at these sizes · What to notice. With a pass: the other 12 sections and 3 interview answers, about 15 minutes.

What you will learn

  • Compute the AND of every integer from m to n from their common binary prefix
  • Prove that the AND of a range keeps only the bits above the highest difference
  • Test for a power of two and find the next one for a buffer size
  • Find the highest 1 bit in each of the 14 languages, with a built-in where there is one
  • Rotate and reverse the 32 bits of a word with masks and with built-ins

Before you start

What you’ll learn · Before you start

What you will learn

  • Compute the AND of every integer from m to n from their common binary prefix
  • Prove that the AND of a range keeps only the bits above the highest difference
  • Test for a power of two and find the next one for a buffer size
  • Find the highest 1 bit in each of the 14 languages, with a built-in where there is one
  • Rotate and reverse the 32 bits of a word with masks and with built-ins

Before you start

On this page

The bitwise AND of a range of integers keeps a bit only if every number from m to n has it, so any bit that changes somewhere inside the range comes out 0: the AND of 26 to 30 is 24. Interviews ask for it because the obvious loop over the range is hopeless at 2³¹ numbers, while the answer takes at most 32 steps. Two related questions ask where a number’s bits are: whether a size is a power of two and which power comes next, and what a word is with its 32 bits reversed. All three follow, each with its brute force and the signal for the fast method.

Three problems about where the bits are

Problem A: the AND of a range. Answer up to 10⁵ queries, each a pair 0 ≤ m ≤ n ≤ 2³¹ − 1: return the bitwise AND of every integer from m to n.

  • (26, 30) gives 24, and (12, 15) gives 12.
  • (5, 5) gives 5, and (1, 2147483647) gives 0.
The numbers 26 to 30 in binary, one bit column at a time: only the 16s and 8s columns are all 1s, so their AND is 11000, which is 24.The AND of 26 to 30, column by column16s8s4s2s1snumber26110102711011281110029111013011110AND11000Each column with a 0 in it ANDs to 0.Only the shared 16s and 8s stay: 11000 = 24.

The AND of the range 26 to 30

Text description of the diagram

The figure writes the numbers 26 to 30 as five bits each, worth 16, 8, 4, 2 and 1: 26 is 11010, 27 is 11011, 28 is 11100, 29 is 11101 and 30 is 11110.

The AND of the range has a 1 in a column only if every number has a 1 there. The 16s and 8s columns, highlighted, are 1 in all five numbers. The 4s column has 0s in 26 and 27, the 2s column a 0 in 28 and 29, and the 1s column 0s in 26, 28 and 30, so those three columns AND to 0. The result is 11000, which is 24.

Problem B: a buffer size. A recording app needs A buffer of fixed size whose write position wraps around to the start when it reaches the end, so the oldest data is overwritten first. whose size is a power of two: A number with exactly one 1 bit in its binary form, such as 1, 2, 4 or 1,024: 2 multiplied by itself some number of times.. For a request of r bytes, 1 ≤ r ≤ 2³⁰, say whether r is already a power of two and return the smallest power of two that is at least r: 1000 gives 1024, and 4096 gives 4096.

Problem C: a reversed word. A sensor sends 32-bit readings with the bit order reversed. Return each reading with its 32 bits reversed, as an unsigned number: 11, which is 1011, gives 3,489,660,928, which is 1101 followed by 28 zeros.

Checkpoint

Check: The AND of a range, buffer sizes and reversed words

  1. Question 1 of 2 What is the AND of every integer from 20 to 23?

    Type a number.

    A hint

    Write 20 to 23 in binary and look for the columns that are 1 in all four.

    Show the answer: Question 1 of 2

    Answer: 20

    20 to 23 are 10100, 10101, 10110 and 10111. The two lowest bits take every value on the way, so they are 0 in the AND, and the bits above them, 101, are the same in all four: the AND is 10100, which is 20.

  2. Question 2 of 2 What is the 32-bit reversal of 1, as an unsigned number?

    Type a number.

    A hint

    The only 1 bit moves from bit 0 to bit 31.

    Show the answer: Question 2 of 2

    Answer: 2147483648

    1 has its only 1 bit at position 0, and the reversal moves it to position 31, so the result is 2³¹ = 2,147,483,648, one of the examples the brute force prints.

The brute force, and why it fails at these sizes

Problem A breaks, as the run’s last two lines show:

The three problems by brute force, with the work counted Python · brute/brute_force.py
# The three problems by brute force. A: AND every integer of the range. B: double until the buffer is big enough.
# C: move the bits one at a time.


def range_and_brute(m, n):
    """Problem A: the AND of every integer from m to n, and the ANDs it took (it stops early at 0)."""
    acc, steps = m, 0
    for x in range(m + 1, n + 1):
        if acc == 0:
            break
        acc &= x
        steps += 1
    return acc, steps


def buffer_size_brute(r):
    """Problem B: whether r is a power of two, and the smallest power of two that is at least r."""
    size = 1
    while size < r:
        size *= 2
    return size == r, size


def reverse_brute(x):
    """Problem C: the 32 bits of x in reverse order, one bit at a time."""
    out = 0
    for _ in range(32):
        out = (out << 1) | (x & 1)
        x >>= 1
    return out


for m, n in ((26, 30), (12, 15), (5, 5), (1, 2**31 - 1)):
    value, steps = range_and_brute(m, n)
    print(f"A: AND of {m}..{n} = {value}  ({steps} {'AND' if steps == 1 else 'ANDs'})")
for r in (1000, 4096, 1):
    print(f"B: request {r}: {buffer_size_brute(r)}")
for x in (11, 1, 0):
    print(f"C: reverse of {x} = {reverse_brute(x)}")

m, n = 2**30, 2**31 - 1
print(f"A on {m:,}..{n:,}: the AND never reaches 0, so {n - m:,} ANDs for one query")
print(f"with 100,000 such queries: {100_000 * (n - m):,} ANDs")

Output

A: AND of 26..30 = 24  (4 ANDs)
A: AND of 12..15 = 12  (3 ANDs)
A: AND of 5..5 = 5  (0 ANDs)
A: AND of 1..2147483647 = 0  (1 AND)
B: request 1000: (False, 1024)
B: request 4096: (True, 4096)
B: request 1: (True, 1)
C: reverse of 11 = 3489660928
C: reverse of 1 = 2147483648
C: reverse of 0 = 0
A on 1,073,741,824..2,147,483,647: the AND never reaches 0, so 1,073,741,823 ANDs for one query
with 100,000 such queries: 107,374,182,300,000 ANDs

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 brute_force.py

ANDing every integer of the range can stop once the result is 0, which saves the query (1, 2147483647), but (2³⁰, 2³¹ − 1) never reaches 0: 1,073,741,823 ANDs for one query, and 10⁵ such queries need 107,374,182,300,000, about 12 days of computing even at 10⁸ ANDs a second. B and C are cheap even by brute force, 30 doublings and 32 bit moves at most, which is why their questions are about doing them in a few operations and getting the edge cases right. Step through the brute force of A on its first query:

Interactive · stage by stage The AND of 26 to 30, one number at a time Step through the range. Each box ANDs one more number into the running value; watch the bits that change somewhere in the range drop out.
The AND of 26 to 30, one number at a time, step 5 of 5. Showing start with 26, AND 27 (11011) (still 11010 = 26), AND 28 (11100) (now 11000 = 24), AND 29 (11101) (still 11000 = 24), AND 30 (11110) (the AND: 11000 = 24). In focus: AND 30 (11110).start with 2611010AND 27 (11011)still 11010 = 26AND 28 (11100)now 11000 = 24AND 29 (11101)still 11000 = 24AND 30 (11110)the AND: 11000 = 24

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. The first number. The running AND starts as 26, which is 11010 in binary.
  2. AND 27. 27 is 11011. It has every 1 that 26 has, so the AND stays 11010, which is 26.
  3. AND 28. 28 is 11100, with a 0 in bit 1, so bit 1 drops out and the AND becomes 11000, which is 24.
  4. AND 29. 29 is 11101. Bits 4 and 3 are still on in it, so the AND stays 11000.
  5. AND 30. 30 is 11110, and the AND is still 11000 = 24. Only the bits above the highest difference of 26 and 30 survived, and the loop took 4 ANDs for 5 numbers; for a range of 2³⁰ numbers it would take more than a billion.

Checkpoint

Check: What the brute force costs at the limits

  1. One question The brute force of A stops early when the AND reaches 0. Why does that not help the query (2³⁰, 2³¹ − 1)?

    Choose one answer.

    Show the answer: One question

    Answer: Every number from 2³⁰ to 2³¹ − 1 has bit 30 on, so the AND never reaches 0

    The early stop saves ranges whose AND falls to 0 quickly. In (2³⁰, 2³¹ − 1) every number has bit 30 on, so the AND stays at 2³⁰ and the loop makes 1,073,741,823 ANDs for one query.

What to notice

  • In A, a bit that flips anywhere in the range is 0 in the AND, and once the numbers differ at some bit, every lower bit flips on the way from m to n. Only the bits above the highest difference of m and n can survive.
  • In B, a power of two has exactly one 1 bit, and the next power of two depends only on the length of r − 1 in binary.
  • In C, the reversal is a fixed reordering of 32 positions, so it can move blocks of bits instead of single bits.

Step through them on small cases:

Interactive · stage by stage The three signals, each on one small case Step through the signals. Each box shows one small case in binary and what the signal says about it.
The three signals, each on one small case, step 3 of 3. Showing A: 26 and 30, B: 64 and 1000, C: reversing 11. In focus: C: reversing 11.A: 26 and 3011010 and 11110B: 64 and 10001000000; 999 has 10 bitsC: reversing 11bit i goes to bit 31 − i

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. Where the ends differ. 26 is 11010 and 30 is 11110. They agree on the top two bits, 11, and first differ at bit 2. Every number from 26 to 30 starts with 11, and each lower bit is 0 in at least one of them, so only the 11 can survive in the AND.
  2. One 1 bit, and a length. 64 is 1000000, a single 1 bit, as in every power of two. For a request of 1000, 999 is 1111100111, 10 bits long, so the smallest power of two that is at least 1000 is 2¹⁰ = 1024.
  3. The same moves for every word. Reversing 32 bits sends bit i to bit 31 − i whatever the value, so 11, which is 1011, becomes 1101 followed by 28 zeros. The moves never depend on the bits themselves, so whole blocks of bits can move together.

Checkpoint

Check: The signals that point to the fast methods

  1. One question What do all powers of two have in common in binary?

    Choose one answer.

    Show the answer: One question

    Answer: Exactly one 1 bit

    A power of two is 1 followed by zeros in binary, so it has exactly one 1 bit, which is the signal for problem B.

The questions below cover the three problems and their brute force, the one you know least first.

Practice that adapts to you

Practise this lesson

9 questions about 9 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 9 Which bits are 1 in the AND of every integer from m to n?

    Choose one answer.

    Show the answer: Question 1 of 9

    Answer: The bits that are 1 in every number of the range

    The AND of a range keeps a bit only when every number from m to n has it, which is why one number with a 0 in a column clears that column.

  2. Question 2 of 9 Why is m & n not the AND of the range from m to n?

    Choose one answer.

    Show the answer: Question 2 of 9

    Answer: A bit can be 1 in both ends and 0 in a number between them, as bit 1 is in 26 and 30 but not in 28

    26 is 11010 and 30 is 11110, so 26 & 30 is 11010, which is 26. But 28, which is 11100, has a 0 in bit 1, so the AND of the whole range has a 0 there too: 24. Every number between the ends counts.

  3. Question 3 of 9 A request of 3,000 bytes needs a ring buffer whose size is a power of two. What is the smallest size that holds it?

    Type a number.

    A hint

    List the powers of two around 3,000.

    Show the answer: Question 3 of 9

    Answer: 4096 bytes

    2048 is too small and 4096 is the next power of two, so the buffer is 4,096 bytes. 4096 itself is already a power of two, so a request of 4,096 would keep its size.

  4. Question 4 of 9 Why is problem A's brute force hopeless at its limits?

    Choose one answer.

    Show the answer: Question 4 of 9

    Answer: One query can take about 10⁹ ANDs, and there are up to 10⁵ queries

    The loop visits every number of the range, up to about 2³¹ of them, and the limits allow 10⁵ queries, which puts the total near 10¹⁴ steps.

  5. Question 5 of 9 Problem B's brute force doubles a size from 1 until it is at least r. How many doublings does it make for r = 1000?

    Type a number.

    A hint

    2¹⁰ is 1024.

    Show the answer: Question 5 of 9

    Answer: 10 doublings

    1, 2, 4, … 512 are all below 1000, and the tenth doubling reaches 1024, so the loop makes 10 doublings. At most 30 are needed for r up to 2³⁰, which is why B is cheap even by brute force.

  6. Question 6 of 9 10⁵ queries of problem A, each a range of about 2³⁰ numbers, at 10⁹ simple steps a second. About how long does the brute force take?

    Choose one answer.

    Show the answer: Question 6 of 9

    Answer: About a day

    The total is about 10¹⁴ ANDs, and 10¹⁴ / 10⁹ = 10⁵ seconds, roughly 28 hours, while the fast methods answer a query in at most 32 steps.

  7. Question 7 of 9 m = 18 (10010) and n = 21 (10101). Which bits can survive in the AND of the range?

    Choose one answer.

    Show the answer: Question 7 of 9

    Answer: Only bit 4, the bit above their highest difference

    18 and 21 agree above bit 2 and differ there, so only bit 4 can survive: the AND of 18 to 21 is 10000, which is 16.

  8. Question 8 of 9 Why does the next power of two depend on the binary length of r − 1 rather than of r?

    Choose one answer.

    Show the answer: Question 8 of 9

    Answer: Because a request that is already a power of two must keep its size, and r − 1 is one bit shorter than r exactly then

    For a power of two r = 2ᵏ, r − 1 is k ones, so its length is k and 1 << k is r itself. For any other r the length of r − 1 equals the length of r, and the result is the next power above r.

  9. Question 9 of 9 Why can a 32-bit reversal move blocks of bits instead of one bit at a time?

    Choose one answer.

    Show the answer: Question 9 of 9

    Answer: Because it is a fixed reordering of 32 positions, the same for every word

    A reversal sends position i to position 31 − i for every word, a fixed reordering, so the work can be done by moving halves, bytes and smaller blocks instead of 32 single bits.

The common prefix: why the AND keeps only the shared high bits Pro: opens with a Pro, Premium or Ultimate pass

In this part:

  • Three ways to find it, each traced on 26 to 30
  • Powers of two: the test, the next one and the highest 1 bit
  • Reversing and rotating 32 bits
  • The code in 14 languages
  • IP address ranges: the AND as a network address
  • Edge cases and common bugs
  • Variants: the OR of a range, powers of four and aligned sizes
  • Exercise: the AND of a range and a reversed word
    • The AND of a range, and a word reversed
  • A practice set with sample answers
  • Interview questions
    • Why is the AND of every integer from m to n equal to the common binary prefix of m and n, followed by zeros?
    • Given the first and last IPv4 address of a range, return the smallest CIDR block that holds both. Your Java version keeps addresses in int, clears the lowest bit of n while n > m, and returns a wrong block for some ranges. Which ranges, why, and how do you fix it?
    • Write next_power_of_two(r) for buffer sizes in Java or C++. What should it return for r = 0, r = 1, r = 2^30 + 1 and r = 2^31, and what does a typical one-line version actually return for each?
    • A decoder must reverse the bit order of 10^8 32-bit words. Compare a 32-step loop, five mask-and-shift swaps, a 256-entry byte table and the built-ins of Java and Go. Which do you choose, and what would you measure before deciding?
  • Key takeaways

3 diagrams · 8 runnable examples (Java, Python) · 1 exercise with sample tests · 3 interview answers · about 36 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Terms in this lesson

power of two
A number with exactly one 1 bit in its binary form, such as 1, 2, 4 or 1,024: 2 multiplied by itself some number of times.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.