code: public static final long nextPermutation(long val) {
      long tmp = val | (val - 1);
      return (tmp + 1) | (((-tmp & -~tmp) - 1) >> (Long.numberOfTrailingZeros(val) + 1));
    }
docs: Compute the lexicographically next bit permutation.
  
  Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of
  N 1 bits in a lexicographical sense. For example, if N is 3 and the bit pattern is 00010011,
  the next patterns would be 00010101, 00010110, 00011001,00011010, 00011100, 00100011, and so
  forth.