next-permutation-bits ( v -- w ) (original) (raw)

next-permutation-bits ( v -- w )

Vocabulary
math.combinatorics.bits

Inputs

v an integer

Outputs

w an integer

Word description
Generates the next bitwise permutation with the same number of set bits, given a previous lexicographical value.

Definition

USING: kernel math ;

IN: math.combinatorics.bits

: next-permutation-bits ( v -- w )
[ dup 1 - bitor 1 + dup ] keep [ dup neg bitand ] bi@ /i
2/ 1 - bitor ;