0

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!
user184868
  • 193
  • 9

1 Answers1

1

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;
}
Falk Hüffner
  • 4,942
  • 19
  • 25
  • wow this is great suggestion, now, is it any way to substitute multiplication on something else here? also it works not in all languages, depending on how mult is handled – user184868 Jan 27 '22 at 23:09
  • actually is just found that amount of modula function, for example mod 37 works realy well and for 64 it is 67 – user184868 Jan 27 '22 at 23:19