Fast hash function from 2^n-1 digit (means, binary 111....) into its sum of bits or other some small range

112 Views Asked by At

I m looking for easy hash-function what would map the number, consist of ones

  • from the right (means, 1,3,7,15 = in binary: 1,11, 111,1111)into in the best way summ of its digits (do not suggest just bit count function, i do not want to rely on that here)
  • into in the best way, its summ of ones (1,2,3,4...) or other relative small nash value, not overlapping with others, simply because the amount of such digits is small - there are only 32 digits including 0 for integers, 64 for longs.) Thanks a lot, dear code hackers!
1

There are 1 best solutions below

2
Falk Hüffner On

One option is to use a multiplication with a value derived from a de Bruijn sequence, as is used to count the number of trailing zeros. For the relevant numbers, this gives a unique result between 0 and 31.

uint32_t hash(uint32_t x) {
    return (0x077CB531 * x) >> 27;
}