home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2044 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  3.1 KB

  1. Path: sparky!uunet!olivea!hal.com!decwrl!cache.crc.ricoh.com!james
  2. From: james@crc.ricoh.com (James Allen)
  3. Newsgroups: comp.programming
  4. Subject: Re: finding 1st one in integer
  5. Summary: Mimimal modulus not always prime
  6. Message-ID: <1992Jul22.013018.19938@crc.ricoh.com>
  7. Date: 22 Jul 92 01:30:18 GMT
  8. References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <LOWRY.92Jul21165844@rotor.watson.ibm.com>
  9. Organization: RICOH California Research Center
  10. Lines: 97
  11.  
  12. In article <LOWRY.92Jul21165844@rotor.watson.ibm.com>
  13.     lowry@watson.ibm.com (Andy Lowry) writes:
  14. ] [someone else asks:]
  15. ] > I need a very efficient way of finding the first bit set ( doesn't matter
  16. ] > which side ) in a 32 bit integer. 
  17. ]
  18. ] > One can make the simplifying assumption that only 1 bit will be
  19. ] > set in the 32 bit integer (if that helps).
  20.  
  21. Or take the slightly more difficult case where at most 1 bit is set.
  22. Call this case "ALLOWZERO".
  23.  
  24. ]This seems to work nicely:
  25. ]
  26. ]int firstone(x)
  27. ]unsigned int x;
  28. ]{
  29. ]  static int keys[] = {
  30. ]    -1,  0,  1, 26,  2, 23, 27, -1,  3, 16,
  31. ]    24, 30, 28, 11, -1, 13,  4,  7, 17, -1,
  32. ]    25, 22, 31, 15, 29, 10, 12,  6, -1, 21,
  33. ]    14,  9,  5, 20,  8, 19, 18 };
  34. ]
  35. ]  return keys[x % 37];
  36. ]}
  37. ]
  38. ]The idea is that you want to use a table lookup, but not on the entire
  39. ]sparse range 1-2^31, so you need to find a hash function that will map
  40. ]the set {2^n: 0 <= n <= 31} uniquely to a reasonably dense range of
  41. ]integers.  Using remainder modulo 37 does the trick, and the table
  42. ]above was generated by a trivial C program.  The -1 entries are
  43. ]unused.
  44. ]
  45. ]There's probably some algebraic way of predicting what divisor to use
  46. ]instead of 37 to get the minimum sized table for a given "word size,"
  47. ]but I'll leave that for somebody else to fill in.  I just started with
  48. ]33 and tried successive numbers until one worked...
  49.  
  50. We may need some help from sci.math to get the algebraic solution,
  51. but I followed Andy's approach for all wordsizes up to 490:
  52.  
  53.        Wordsize        Minimum Modulus
  54.        2               2 (or 3 if ALLOWZERO)
  55.        3                4 (or 5 if ALLOWZERO)
  56.        4               5
  57.        5 -    6           9
  58.        7 -   10          11
  59.       11 -   12          13
  60.       13 -   18          19
  61.       19 -   20          25
  62.       21 -   28          29
  63.       29 -   36          37
  64.       37 -   52          53
  65.       53 -   58          59
  66.       59 -   60          61
  67.       61 -   66          67
  68.       67 -   82          83
  69.       83 -  100         101
  70.      101 -  106         107
  71.      107 -  110         121
  72.      111 -  130         131
  73.      131 -  138         139
  74.      139 -  148         149
  75.      149 -  162         163
  76.      163 -  172         173
  77.      173 -  178         179
  78.      179 -  180         181
  79.      181 -  196         197
  80.      197 -  210         211
  81.      211 -  226         227
  82.      227 -  268         269
  83.      269 -  292         293
  84.      293 -  316         317
  85.      317 -  346         347
  86.      347 -  348         349
  87.      349 -  372         373
  88.      373 -  378         379
  89.      379 -  388         389
  90.      389 -  418         419
  91.      419 -  420         421
  92.      421 -  442         443
  93.      443 -  460         461
  94.      461 -  466         467
  95.      467 -  490         491
  96.  
  97. ]        ...But I don't believe
  98. ]it can be a coincidence that 37 is the least prime greater than 32!
  99.  
  100. Well, 17 is the least prime greater than 13.
  101. There are four nonprimes in the above "Minimal Moduli" although
  102. they are all squares of primes: 4, 9, 25, 121.
  103.  
  104. ]--
  105. ]Andy Lowry, lowry@watson.ibm.com, (914) 784-7925
  106. ]IBM Research, P.O. Box 704, Yorktown Heights, NY  10598
  107.  
  108. James Allen, james@crc.ricoh.com
  109.