Gosper Successors: Move a Carry, Repack the Low Ones
2026-11-18

Gosper Successors: Move a Carry, Repack the Low Ones

Gosper Successors generate the next larger integer mask with the same number of one bits. The operation raises a movable bit and packs the remaining low one bits into their smallest possible positions. It enumerates fixed-size subsets in numeric mask order. It does not promise that consecutive masks differ in exactly one bit.

The goal fixes both ordering and population count

Assume a nonzero unsigned mask x and a declared active width n. The desired result is the least integer greater than x with the same population count, provided it still fits within n bits. The ordering is ordinary numeric ordering of the represented masks. The subset interpretation assigns each bit position to a distinct selectable item.

Keeping the number of selected items fixed does not keep every selection position unchanged. A carry may move one chosen position upward and force several lower chosen positions to move. Comparing only the population counts verifies one invariant; verifying the next-successor property also requires that the low tail be made as small as possible after the necessary upward move.

44binary101100and49binary110001both3onesbutfourbitschange.
FixedpopcountnumericnextnotGrayadjacency; activewidth6. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The lowest set bit starts a controlled carry

In unsigned modular arithmetic, compute c as x bitwise AND the unsigned negation of x. This isolates the lowest set bit. Adding c to x forms r, moving a carry through the rightmost run of one bits and into the zero immediately above that run. Some of the old low ones disappear during the carry.

The remaining task is to restore exactly the needed number of one bits at the low end. A familiar form uses r bitwise XOR x, shifts that changed-bit pattern right by two, divides by c, and combines the result with r using bitwise OR. The arithmetic is arranged to reconstruct the population count without undoing the smallest necessary upward carry.

A six-bit example makes the repacked tail visible

Take the independent mask one-zero-one-one-zero-zero, representing forty-four with three one bits. Its lowest set bit c is four. Adding four gives r equal to forty-eight, written one-one-zero-zero-zero-zero. The changed-bit pattern from r XOR x is twenty-eight, or zero-one-one-one-zero-zero in six bits.

Shifting that pattern right by two gives seven, and integer division by c equal to four gives one. Combining that low one with r gives forty-nine, written one-one-zero-zero-zero-one. Both masks contain three ones, and forty-nine is the least larger mask with that count. The transition changes four bit positions, showing why fixed population count is different from one-bit adjacency.

x44c4r48XOR28shiftright2divide4tail1OR49.
Unsignednonzeroinputguard,widthandoverflowseparatecontract. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The low tail must be minimal after the carry

Once a bit is raised to the first available higher position, any required lower one bits must be packed into the least significant available places. Putting them farther left would give a larger valid mask and skip an earlier candidate. Keeping a lower high-order prefix instead would not provide a number greater than the current mask at the necessary first differing position.

The cited Stanford collection presents an alternative expression using a mask formed from x OR x minus one and a trailing-zero count, as well as a division-based variant. Equivalent formulas can arrange temporary values differently. Our illustrated c-and-r expression follows the independently authored derivation cited alongside it; the intermediate variable names should not be mixed across versions.

Zero input and the active-width boundary need guards

For x equal to zero, c is zero, so the displayed division is invalid. If the requested subset size is zero, the sole valid mask is zero and there is no next nonzero mask with the same population count. That case belongs outside the nonzero successor calculation. An implementation also needs a decision for the final valid mask.

For width six and three selected positions, the largest valid mask is one-one-one-zero-zero-zero, or fifty-six. Its mathematical next fixed-count mask uses a seventh bit and falls outside the active width. A wrapper must stop rather than silently accept that result. If the storage word itself overflows during the carry, checking the mathematical result afterward may already be too late.

Unsigned semantics are part of a portable translation

Signed overflow, negating a minimum signed value and shifting by the full machine-word width have language-specific hazards. The conceptual arithmetic should therefore be connected to a properly bounded unsigned implementation. Operation costs depend on the actual processor, compiler and integer type; a language-specific rule for negating an unsigned value also needs its own verification.

The site's bit enumeration principles and technical features offer context for masks and subsets. Primary authored sources: Sean Anderson's original next-bit-permutation collection and the original c-and-r derivation. Both figures are original AI-assisted arithmetic diagrams, without a processor benchmark or a hardware transition claim.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我