// -*- C++ -*-

// ===== FUNCTIONS declared in auxil/auxbit.h: =====

inline ulong rotate_left(ulong x, ulong s);

inline ulong rotate_right(ulong x, ulong s);

inline ulong graycode(ulong x);

inline ulong inverse_graycode(ulong x);
// inverse if graycode()

inline ulong parity(ulong x);

inline ulong bit_count(ulong x);
// return number of bits set

inline ulong bit_count_sparse(ulong x);
// return number of bits set
// the loop will execute once for each bit of x set

inline ulong highest_bit(ulong x);
// return index of highest bit set
// return ~0 if no bit is set

inline ulong lowest_bit(ulong x);
// return index of lowest bit set
// return ~0 if no bit is set

inline ulong ld(ulong x);
// returns k so that 2^k <= x < 2^(k+1)

inline ulong ldq(ulong x);
// return k if x=2**k
// else return ~0

inline unsigned long long ld(unsigned long long x);
// returns k so that 2^k <= x < 2^(k+1)

inline int is_pow_of_2(ulong x);
// return 1 if x == 0(!) or x == 2**k

inline int one_bit_q(ulong x);
// return 1 iff x \in {1,2,4,8,16,...}

inline ulong next_pow_of_2(ulong x);
// return x if x=2**k
// else return 2**ceil(log_2(x))

inline ulong next_exp_of_2(ulong x);
// return k if x=2**k
// else return k+1

inline ulong next_comb(ulong w);
// return smallest integer greater than u with the same number of bits set.
//
//  examples (binary notation):
//    1 -> 10 -> 100 -> 1000 -> 10000 -> 100000 -> 1000000 -> ...
//    11 -> 101 -> 110 -> 1001 -> 1010 -> 1100 -> 10001 -> ...
//    111 -> 1011 -> 1101 -> 1110 -> 10011 -> 10101 -> 10110 -> ...
//
//  special cases:
//    0 -> 0
//    if the number is too large then 0 is returned.
//
//  The bit pattern to change at the end of the word is:
//    0 1   0   ->    1 0   1
//       a   b           b+1 a-1
//
//  Example: (the bits marked by B remain fixed)
//    w       = BBBB01111100000000
//    z       = 000000000100000000   (the lowest set bit)
//    w+z     = BBBB10000000000000
//    (w+z)^w = 000011111100000000
//    next    = BBBB10000000001111
//
//  this was shamelessly stolen from Torsten Sillke's bitman.h
//  (the algorithm can be found in hakmem)

